Create claims identity in Identity 3
UserManager has changed in the MVC6 version. You will need to modify your code...
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
var authenticationType = "Put authentication type Here";
var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authenticationType);
// Add custom user claims here
return userIdentity;
}
.net core
the answer has changed, per here and here, both authors state the use UserClaimsPrincipalFactory<ApplicationUser>
which is the default implementation for core 2.2. The first article says that the method you are looking for has moved. However, as stated you must register your implementation of UserClaimsPrincipalFactory
in services like so and a sample class implementation is below. Please take that we have to register MyUserClaimsPrincipalFactory
so our service collection knows where to find it. Which means in the constructor of SignInManager<ApplicationUser>
it is also referring to IUserClaimsPrincipalFactory<ApplicationUser>
but service will resolve it:
services
.AddIdentity<ApplicationUser, ApplicationRole>()
.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>() // <======== HERE
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, MyUserClaimsPrincipalFactory>();
And here is is the class below:
public class MyUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
public MyUserClaimsPrincipalFactory(UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor)
: base(userManager, optionsAccessor)
{
}
protected override async Task<ClaimsIdentity> GenerateClaimsAsync(ApplicationUser user)
{
var identity = await base.GenerateClaimsAsync(user);
identity.AddClaim(new Claim("ContactName", "John Smith"));
return identity;
}
}