如何强制Asp.Net Identity 2.0使用自定义角色而不是IdentityUserRole

Lea*_*rve 3 asp.net-mvc roles asp.net-identity asp.net-identity-2

我扩展了IdentityUserRole,如下所示

public class ApplicationUserRoles : IdentityUserRole<string>
{
    [Key]
    public string ApplicationId { get; set; }

    public virtual AspNetApplications AspNetApplications { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的AspNetApplications类如下

public class AspNetApplications
{
    [Key]
    public string ApplicationId { get; set; }
    public string ApplicationName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

迁移已在DB中创建了AspNetApplications和ApplicationUserRoles表.屏幕截图如下.

在此输入图像描述

以下是我的身份模型

public class ApplicationUser : IdentityUser
{
    public virtual AspNetApplications AspNetApplication { get; set; }
    public virtual ApplicationUserRoles AspNetUserRoles { get; set; }
    //public virtual ICollection<AspNetApplicationUsers> AspNetApplicationUsers { 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 class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<AspNetApplications> AspNetApplications { get; set; }
    public DbSet<ApplicationUserRoles> AspNetUserRoles { get; set; }

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

        modelBuilder.Entity<ApplicationUserRoles>().HasKey(m => new { m.ApplicationId, m.UserId, m.RoleId });
    }

}
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好但现在当我在我的帐户控制器中检查用户时,它仍然从AspNetUserRoles带来基于角色的信息.可以请任何人告诉我如何使用我的自定义ApplicationUserRoles而不是IdentityUserRole.

Lef*_*tyX 8

如果你看一下签名IdentityUser(你的ApplicationUser继承自它)

public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
    where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
    where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{

}
Run Code Online (Sandbox Code Playgroud)

你可以看到它接受你的自定义定义IdentityUserRole.

MyApplicationUser必须实现的类必须实现正确的类型:

public class MyApplicationUser : IdentityUser<string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{

}
Run Code Online (Sandbox Code Playgroud)

和你的角色:

public class MyRole : IdentityRole<string, ApplicationUserRoles>
{
}
Run Code Online (Sandbox Code Playgroud)

和你的ApplicationDbContext:

public class MyContext : IdentityDbContext<MyUser, MyRole, string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{

}
Run Code Online (Sandbox Code Playgroud)

和你的UserStore:

public class MyUserStore:  UserStore<MyUser, MyRole, string, MyUserLogin, ApplicationUserRoles, MyUserClaim>
{
    public MyUserStore(MyContext context)
        : base(context)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

应该是这样,我想.有一个github repo,我用自定义类和自定义表名称玩了一下.