Net Core:找不到类型或命名空间名称“RoleProvider”

问题描述 投票:9回答:1

我正在将项目从.Net 4.6.2迁移到.Net Core 2.0。 Net Core中RoleProvider的替代品是什么?

找不到类型或命名空间名称“RoleProvider”(您是否缺少using指令或程序集引用?)

 public class CustomerRoleProvider : RoleProvider
    {
        public override string CustomerName { get; set; }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {

新代码看起来像这样,收到错误

Using the generic type 'RoleManager<TRole>' requires 1 type arguments

// Error first line: Using the generic type 'RoleManager<TRole>' requires 1 type arguments

public class CustomerRoleProvider : RoleManager
{
    public string ApplicationName { get; set; }

    public void CreateRole(IServiceProvider serviceProvider, string roleName)
    {
        var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        throw new NotImplementedException();
    }

更新:John Kenney的回答看起来很棒,希望有人可以在他的回答中添加更多内容作为编辑。

c# .net-core asp.net-core-mvc .net-core-2.0
1个回答
5
投票

RoleProvider在.NET Core中不存在,至少不是那种形式。我相信你要找的是RoleManager。实现与以下内容非常相似:

var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();

有关如何实现它的详细信息,请参阅此article

© www.soinside.com 2019 - 2024. All rights reserved.