Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create claims identity in Identity 3

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.

like image 433
Ungaro Avatar asked Apr 10 '16 10:04

Ungaro


People also ask

What is a claim identity?

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.

What are claims in Identity Server?

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.


2 Answers

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;
}
like image 91
Nkosi Avatar answered Sep 22 '22 02:09

Nkosi


.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;
    }
}
like image 26
petrosmm Avatar answered Sep 21 '22 02:09

petrosmm