Skip to content

Commit

Permalink
Fixes #11189
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed Sep 25, 2021
1 parent cf52b46 commit 7f2d2b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Umbraco.Web.Common/Security/ConfigureMemberCookieOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

namespace Umbraco.Cms.Web.Common.Security
{
Expand Down Expand Up @@ -34,6 +36,19 @@ public void Configure(CookieAuthenticationOptions options)
options.LogoutPath = null;

options.CookieManager = new MemberCookieManager(_runtimeState, _umbracoRequestPaths);

options.Events = new CookieAuthenticationEvents
{
OnSignedIn = ctx =>
{
// occurs when sign in is successful and after the ticket is written to the outbound cookie

// When we are signed in with the cookie, assign the principal to the current HttpContext
ctx.HttpContext.SetPrincipalForRequest(ctx.Principal);

return Task.CompletedTask;
}
};
}
}
}
15 changes: 15 additions & 0 deletions src/Umbraco.Web.Website/Routing/PublicAccessRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
Expand Down Expand Up @@ -65,6 +67,19 @@ public async Task<UmbracoRouteValues> RewriteForPublishedContentAccessAsync(Http
{
_logger.LogDebug("EnsurePublishedContentAccess: Page is protected, check for access");

// manually authenticate the request
AuthenticateResult authResult = await httpContext.AuthenticateAsync(IdentityConstants.ApplicationScheme);
if (authResult.Succeeded)
{
// set the user to the auth result. we need to do this here because this occurs
// before the authentication middleware.
// NOTE: It would be possible to just pass the authResult to the HasMemberAccessToContentAsync method
// instead of relying directly on the user assigned to the http context, and then the auth middleware
// will run anyways and assign the user. Perhaps that is a little cleaner, but would require more code
// changes right now, and really it's not any different in the end result.
httpContext.User = authResult.Principal;
}

publicAccessStatus = await _publicAccessChecker.HasMemberAccessToContentAsync(publishedContent.Id);
switch (publicAccessStatus)
{
Expand Down

0 comments on commit 7f2d2b3

Please sign in to comment.