我正在使用ASP.Net MVC和Identity 2.0开发一个多租户Web应用程序。我已经扩展了IdentityRole,就像这样:
public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name) : base(name) { }
public string TenantId { get; set; }
}
这是因为每个租户都有各自的角色集,比如“管理员”、“员工”等。
但问题在于,当我添加一个新角色时,如果“租户A”有“管理员”角色,当我把“管理员”角色添加到“租户B”时,我会得到一个IdentityResult错误,因为“管理员”名称已被占用…很明显,因为AspNetRoles表中的“Name”字段是唯一的…
IdentityResult roleResult = await RoleManager.CreateAsync(
new ApplicationRole
{
Name = "Admin",
TenantId = GetTenantId()
});
那么我应该如何定制ASP.Net Identity,使得“AspNetRoles”中的“Name”字段可以与“TenantId”一起成为唯一值而不是单独的?我找到了有关扩展IdentityRole(就像我添加字段一样)的信息,但没有关于更改或替换它的信息...