网络核心:类型或名称空间名称“ RoleProvider”找不到

8 c# asp.net-core-mvc .net-core .net-core-2.0

我正在将一个项目从.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)
        {
Run Code Online (Sandbox Code Playgroud)

新代码如下所示,收到错误

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();
    }
Run Code Online (Sandbox Code Playgroud)

更新: 约翰·肯尼(John Kenney)的回答看起来很棒,希望有人可以在编辑时添加更多内容。

Joh*_*edy 5

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

var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
Run Code Online (Sandbox Code Playgroud)

请参阅本文以获取有关如何实现它的详细信息。

  • 这个答案很好,希望它不仅可以提供更多解释,还可以链接https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers (2认同)