Skip to content

Commit

Permalink
Maintain claims principal during refresh (OrchardCMS#14918)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored and urbanit committed Mar 18, 2024
1 parent 8d141c7 commit 4c2b6b5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Users/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public override void ConfigureServices(IServiceCollection services)
options.LogoutPath = "/" + userOptions.Value.LogoffPath;
options.AccessDeniedPath = "/Error/403";
});

services.AddTransient<IPostConfigureOptions<SecurityStampValidatorOptions>, ConfigureSecurityStampOptions>();
services.AddDataMigration<Migrations>();

services.AddScoped<IUserClaimsProvider, EmailClaimsProvider>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;

namespace OrchardCore.Users.Services;

public class ConfigureSecurityStampOptions : IPostConfigureOptions<SecurityStampValidatorOptions>
{
public void PostConfigure(string name, SecurityStampValidatorOptions options)
{
options.OnRefreshingPrincipal = principalContext =>
{
var currentIdentity = principalContext.CurrentPrincipal?.Identities?.FirstOrDefault();
if (currentIdentity is not null && principalContext.NewPrincipal.Identities is not null)
{
var newIdentity = principalContext.NewPrincipal.Identities.First();
foreach (var claim in currentIdentity.Claims)
{
if (newIdentity.HasClaim(claim.Type, claim.Value))
{
continue;
}
newIdentity.AddClaim(new Claim(claim.Type, claim.Value));
}
}
return Task.CompletedTask;
};
}
}

0 comments on commit 4c2b6b5

Please sign in to comment.