【问题标题】:How to Override base functionality of UserManager.GetRolesAsync(TKey userId)如何覆盖 UserManager.GetRolesAsync(TKey userId) 的基本功能
【发布时间】:2015-10-07 08:13:55
【问题描述】:

GetRolesAsync(TKey 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();
    }

有谁知道如何在 UserManager 的派生类中重写此功能,甚至提供一个新的方法,如 GetModelRolesAsync(string userId) 来返回一个角色模型。

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; }
}

【问题讨论】:

    标签: c# asp.net user-roles asp.net-roles security-roles


    【解决方案1】:

    Asp.Net 身份实体框架库提供了一个开箱即用的身份角色模型,称为IdentityRole。您可以将其与提供的 RoleManager Class 结合使用以返回 IdentityRole 模型。

    您必须提供自己的函数,Task&lt;IList&lt;string&gt;&gt; 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; 
         }      
    }
    

    您可以将系统配置为使用 ASP.NET 内置的 RoleManager 依赖注入系统,如 here 所示。

    【讨论】:

    • 完美!谢谢,只是似乎无法连接那个点!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多