Extending IdentityRole and IdentityUser

此生再无相见时 提交于 2019-12-05 01:53:58

问题


I am using ASPNet Identity to implement security in my web application.

There is a requirements where in, I need to extend the IdentityRole and IdentityUser.

Here is my code to extend the IdentityUser.

public class ApplicationUser : IdentityUser
{
    public virtual User  User { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("name=CoreContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>()
            .ToTable("AspNetUsers");
        modelBuilder.Entity<ApplicationUser>()
            .ToTable("AspNetUsers");
    }
}

My only problem is the IdentityRole


回答1:


To extend User , Update your ApplicationUser class(located in IdentityModels.cs) to

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> 
        GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager
            .CreateIdentityAsync(this, 
                DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }

    // Use a sensible display name for views:
    [Display(Name = "Postal Code")]
    public string PostalCode { get; set; }

}

To Extend Role , Create a class ApplicationRole.cs

public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }
    public ApplicationRole(string name) : base(name) { }
    public string Description { get; set; }
}

and Add the class inside IdentityConfig.cs file :

public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
    public ApplicationRoleManager(
        IRoleStore<ApplicationRole,string> roleStore)
        : base(roleStore)
    {
    }
    public static ApplicationRoleManager Create(
        IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(
            new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
    }
}

Now clear the old database, Run the application and register a user. It will create 3 more fields(Address,City,State) in AspNetUsers table and one more field(Description) into AspNetRoles table. That's it.

For more info , go to the site : Extending Identity Role




回答2:


You can inherit from IdentityRole in your application the same way that you do your IdentityUser. Why do you need to extend IdentityRole? Please take a look at the following article which explains in detail what you are trying to do http://typecastexception.com/post/2014/02/13/ASPNET-MVC-5-Identity-Extending-and-Modifying-Roles.aspx



来源:https://stackoverflow.com/questions/22057403/extending-identityrole-and-identityuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!