Visual Studio 2015 scaffolding uses UserManager<TUser>
which cannot be used to create ClaimsIdentity
. Does anyone have a working example on how to do this?
The VS2015 scaffolding throws errors:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one
// defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
N.B.: I have added properties to ApplicationUser
which do not conflict with IdentyUser
.
Claims-based identity is a means of authenticating an end user, application or device to another system in a way that abstracts the entity's specific information while providing data that authorizes it for appropriate and relevant interactions.
A claim is statement that a particular entity has a particular property. The claim is asserted by some entity - the asserting party states that the subject has some attribute. In authentication, we usually think of claims as assertions about a user, as asserted by the Identity Provider.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With