I'm trying to use IdentityServer4 with ASP.Net Identity Core providing a user store. I've loosely followed this guide: https://www.scottbrady91.com/Identity-Server/Getting-Started-with-IdentityServer-4, and have gotten to a point where I can register and authenticate users locally using ASP.Net Identity.
My problem is that I add some claims on registration:
var createUserResult = await _userManager.CreateAsync(user, model.Password);
if (createUserResult.Succeeded)
{
var claims = new List<Claim>
{
new Claim(JwtClaimTypes.Email, user.Email),
new Claim(JwtClaimTypes.Role, "user"),
new Claim("test-claim", "loremipsum")
};
await _userManager.AddClaimsAsync(user, claims);
await HttpContext.SignInAsync(user.Id, user.UserName, new AuthenticationProperties());
return Redirect(model.ReturnUrl ?? "~/");
}
These are saved correctly, and I can see them in the database. However once I login, the user object only has the following claims:
- sub
- name
- auth_time
- idp
- amr
Looking online, it seems that I need to override the UserClaimsPrincipalFactory and register this implementation with the DI container:
public class CustomUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
public CustomUserClaimsPrincipalFactory(
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
IOptions<IdentityOptions> options) : base(userManager, roleManager, options)
{
Console.WriteLine("Test");
}
public override async Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
{
var principal = await base.CreateAsync(user);
((ClaimsIdentity) principal.Identity).AddClaim(new Claim("yolo", "swag"));
return principal;
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("yolo", "swag"));
return identity;
}
}
Registered in the DI container as follows:
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, CustomUserClaimsPrincipalFactory>();
My understanding is that this custom principal factory should add the claims I want to the User object, but this doesn't happen. I can see the custom claims principal factory is instantiated using a breakpoint in the constructor, but the GenerateClaimsAsync function is never called.
IdentityServer and ASP.Net identity are registered as follows:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer(config =>
{
config.PublicOrigin = _configuration.PublicOriginUrl;
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder => builder.UseNpgsql(
_sqlConnectionString,
sqlOptions => sqlOptions.MigrationsAssembly(MigrationsAssembly));
})
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder => builder.UseNpgsql(
_sqlConnectionString,
sqlOptions => sqlOptions.MigrationsAssembly(MigrationsAssembly));
})
.AddAspNetIdentity<ApplicationUser>()
.AddDeveloperSigningCredential();
My custom user claims principal factory is registered after this section. Looking at the AddAspNetIdentity extension method here, I can see it seems to register some sort of custom user claims principal factory, but I don't understand the implementation code well enough to work out exactly what's happening.
How can I implement a custom user claims principal factory such that I could do something like this in a view?
// "yolo" is a custom claim
@User.FindFirst("yolo")

AddAspNetIdentityextension method again, and I now understand that theUserPrincipalClaimsFactoryimplementation that I give is passed into IdentityServer'sUserClaimsFactory<TUser>class, and this class doesn't have aGenerateClaimsAsyncmethod. However, I'm not sure why this stops my implementation'sGenerateClaimsAsyncbeing called, even if I specifically re-register it in the DI container after adding IdentityServer. - BnMcG