Add custom field to MVC SimpleMembership roles

254 Views Asked by At

I've got this code to define my ApplicaitonUser. Name and Created are custom fields I have added.

How do I do the same thing for the Roles? I need to add an integer field to them.

public class ApplicationUser : IdentityUser
{
    public DateTime Created { get; set; }
    public string Name { get; set; }

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

    public ApplicationUser()
    {
        Created = DateTime.UtcNow;
    }
}
1

There are 1 best solutions below

0
Rupa Mistry On

You can add a custom field to the Roles table by deriving from IdentityRole class.

Code snippet:

    public class ApplicationRole : IdentityRole
    { 
       public ApplicationRole() : base() 
       {
       }

       // New property
       public int MasterRoleId{ get; set; }
    }