在 ASP.NET 身份中自定义 UserRole
Customising UserRole in ASP.NET identity
从开箱即用的 ASP.NET 身份开始,我得到了
public class ApplicationUser : IdentityUser
{
public int OrganisationId { get; set; }
}
并已进行并应用 迁移,已将此列添加到 AspNetUsers
table。
现在我想维护一个组织层次结构,让角色成员继承该层次结构。
所以我要
public class ApplicationUserRole : IdentityUserRole<string>
{
public int OrganisationId { get; set; }
public bool Cascade { get; set; }
}
然而,这似乎是不可能的,因为 IdentityUserRole
不是 IdentityDbContext
的类型参数之一,而且似乎会造成 IUserRoleStore
的各种问题。
是否有更好的方法来实现与 ASP.NET 身份更兼容的要求?
存在三种不同的 IdentityDbContext
class,您正在寻找 IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim>
,您可以使用它来将泛型类型参数更改为您的自定义角色 class。
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationUserRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
/// Existing code
}
对 UserStore
进行完全相同的自定义 can/should 并且在所有不使用内置身份类型的地方。
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationUserRole, , string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
/// Possible overrides
}
如果您只是继承并使用您的自定义 classes,它应该开箱即用,无需任何过度开发。
相关链接:
从开箱即用的 ASP.NET 身份开始,我得到了
public class ApplicationUser : IdentityUser
{
public int OrganisationId { get; set; }
}
并已进行并应用 迁移,已将此列添加到 AspNetUsers
table。
现在我想维护一个组织层次结构,让角色成员继承该层次结构。
所以我要
public class ApplicationUserRole : IdentityUserRole<string>
{
public int OrganisationId { get; set; }
public bool Cascade { get; set; }
}
然而,这似乎是不可能的,因为 IdentityUserRole
不是 IdentityDbContext
的类型参数之一,而且似乎会造成 IUserRoleStore
的各种问题。
是否有更好的方法来实现与 ASP.NET 身份更兼容的要求?
存在三种不同的 IdentityDbContext
class,您正在寻找 IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim>
,您可以使用它来将泛型类型参数更改为您的自定义角色 class。
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationUserRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
/// Existing code
}
对 UserStore
进行完全相同的自定义 can/should 并且在所有不使用内置身份类型的地方。
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationUserRole, , string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
/// Possible overrides
}
如果您只是继承并使用您的自定义 classes,它应该开箱即用,无需任何过度开发。
相关链接: