How to extend IdentityUser with custom property

后端 未结 3 1230
梦毁少年i
梦毁少年i 2020-11-29 22:20

I\'m using asp.net Identity 2.0 for users to log into my website, where the authentication details are stored in an SQL database. Asp.net Identity has been implemented in a

3条回答
  •  温柔的废话
    2020-11-29 22:54

    I think the main problem still is you don't register the new claims at the ApplicationUser class.

    I was having the same problem and I solved it with a few lines after the CreateIdentityAsync.

    public class ApplicationUser : IdentityUser
    {
        public async Task GenerateUserIdentityAsync(UserManager manager, string authenticationType)
        {
           CookieAuthenticationOptions.AuthenticationType
           var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
    
           // Add custom user claims here
            userIdentity.AddClaim(new Claim("Code",this.Code));
           return userIdentity;
        }
        //My extended property
        public string Code { get; set; }
    }
    

提交回复
热议问题