How to extend IdentityUser with custom property

后端 未结 3 1234
梦毁少年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:57

    Hope this might help others, since the original post is 1+years old

    If have already created the project with 'Authentication Individual User Accounts:

    In Solution Explorer go to project>Models>IdentityModels.cs

    under public class ApplicationUser: IdentityUser (should be the first class).

    Add your custom properties after public async Task GenerateUserIdentityAsync(UserManager manager) like my example below:

    public class ApplicationUser : IdentityUser
    {
        public async Task GenerateUserIdentityAsync(UserManager 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;
        }
        **//add custom properties here - these are just examples**
        public bool SendEmails { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    

    Then using NuGet Packagemanager:

    • enable-migrations (if you haven't already)
    • add-migration [enter]
    • nameYourMigration [enter]
    • (view the migration file to verify your properties are going to be added)
    • update-database [enter]
    • Check your AspNetUsers db table to be sure the properties were added correctly

    I hope this helps..

提交回复
热议问题