Skip to content

Commit

Permalink
Maintain claims principal during refresh
Browse files Browse the repository at this point in the history
Fix #14917
  • Loading branch information
MikeAlhayek committed Dec 18, 2023
1 parent f28560f commit 2a357b6
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<IConfigureOptions<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 : IConfigureOptions<SecurityStampValidatorOptions>
{
public void Configure(SecurityStampValidatorOptions options)
{
options.OnRefreshingPrincipal = principalContaxt =>
{
var currentIdentity = principalContaxt.CurrentPrincipal?.Identities?.FirstOrDefault();
if (currentIdentity is not null && principalContaxt.NewPrincipal.Identities is not null)
{
var newIdentity = principalContaxt.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 2a357b6

Please sign in to comment.