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

2024-05-13

我已经扩展了 IdentityUserRole 如下

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

    public virtual AspNetApplications AspNetApplications { get; set; }
}

我的 AspNetApplications 类如下

public class AspNetApplications
{
    [Key]
    public string ApplicationId { get; set; }
    public string ApplicationName { get; set; }
}

迁移已在数据库中创建了 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 });
    }

}

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


如果你看一下签名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>
{

}

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

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

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

}

以及你的角色:

public class MyRole : IdentityRole<string, ApplicationUserRoles>
{
}

和你的ApplicationDbContext:

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

}

和你的UserStore:

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

我想应该是这样。有一个githubrepo https://github.com/Leftyx/ASP.NET-Identity-2.x我在那里玩了一下自定义类和自定义表名称。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐