Search code examples
razorentity-framework-6asp.net-coreasp.net-identity-3

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.


Solution

  • 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;
    }