C# 如何覆盖UserManager.GetRolesAsync(TKey userId)的基本功能
GetRolesAsync(TKey userId)的基本功能如下C# 如何覆盖UserManager.GetRolesAsync(TKey userId)的基本功能,c#,asp.net,user-roles,asp.net-roles,security-roles,C#,Asp.net,User Roles,Asp.net Roles,Security Roles,GetRolesAsync(TKey userId)的基本功能如下 public virtual async Task<IList<string>> GetRolesAsync(TKey userId) { ThrowIfDisposed(); var userRoleStore = GetUserRoleStore(); var user = await FindByIdAsync(userId).
public virtual async Task<IList<string>> GetRolesAsync(TKey userId)
{
ThrowIfDisposed();
var userRoleStore = GetUserRoleStore();
var user = await FindByIdAsync(userId).WithCurrentCulture();
if (user == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.UserIdNotFound,
userId));
}
return await userRoleStore.GetRolesAsync(user).WithCurrentCulture();
}
public虚拟异步任务GetRolesAsync(TKey userId)
{
ThrowIfDisposed();
var userRoleStore=GetUserRoleStore();
var user=wait FindByIdAsync(userId).WithCurrentCulture();
if(user==null)
{
抛出新的InvalidOperationException(String.Format(CultureInfo.CurrentCulture,Resources.UserIdNotFound,
用户ID);
}
return await userRoleStore.GetRolesAsync(user).WithCurrentCulture();
}
是否有人知道如何在UserManager的派生类中重写此功能,或者甚至提供诸如GetModelRolesAsync(string userId)之类的新方法来返回roleModel
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
public override async Task<IList<RoleModel>> GetRolesAsync(string userId)
{
// Need code here to return a RoleModel that includes the ID
// as well as the role name, so a complex object instead of just
// a list of strings
}
}
public class RoleModel
{
public string Id { get; set; }
public string Name { get; set; }
}
公共类应用程序管理员:用户管理员
{
公共应用程序服务器管理器(IUserStore存储)
:基地(商店)
{
}
公共静态应用程序SerManager创建(IdentityFactoryOptions选项,IOwinContext上下文)
{
var manager=newapplicationUserManager(newuserstore(context.Get());
//为用户名配置验证逻辑
manager.UserValidator=新的UserValidator(管理器)
{
AllowOnlyAlphanumericUserNames=false,
RequireUniqueEmail=true
};
//配置密码的验证逻辑
manager.PasswordValidator=新密码验证器
{
所需长度=6,
RequiredOnletterDigit=真,
RequireDigit=true,
RequireLowercase=true,
RequireUppercase=true,
};
var dataProtectionProvider=options.dataProtectionProvider;
if(dataProtectionProvider!=null)
{
manager.UserTokenProvider=newdataprotectortokenprovider(dataProtectionProvider.Create(“ASP.NET标识”);
}
退货经理;
}
公共重写异步任务GetRolesAsync(字符串用户ID)
{
//需要在此处输入代码以返回包含ID的RoleModel
//以及角色名称,因此是一个复杂的对象,而不仅仅是
//字符串列表
}
}
公共类角色模型
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
}
Asp.Net Identity Entity Framework库提供了一个现成的身份角色模型,名为。您可以将其与提供的结合使用,以返回IdentityRole
模型
不过,您必须提供自己的函数,Task GetRolesAsync(TKey userId)
的接口在基类中设置为仅返回字符串
以下是一个例子:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
private RoleManager<IdentityRole> _roleManager;
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
_roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
}
public async Task<IList<IdentityRole>> GetModelRolesAsync(string userId)
{
IList<string> roleNames = await base.GetRolesAsync(userId);
var identityRoles = new List<IdentityRole>();
foreach (var roleName in roleNames)
{
IdentityRole role = await _roleManager.FindByNameAsync(roleName);
identityRoles.Add(role);
}
return identityRoles;
}
}
公共类应用程序管理员:用户管理员。完美!谢谢你,只是好像无法把这一点连起来!