简体   繁体   中英

Net Core: Type or namespace name 'RoleProvider' could not be found

I am migrating a project from .Net 4.6.2 into .Net Core 2.0. What is the replacement for RoleProvider in Net Core?

The type or namespace name 'RoleProvider' could not be found (are you missing a using directive or an assembly reference?)

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

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

New code looks like this, received error

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

Update: Answer from John Kenney looks great, hopefully someone can add more into his answer as edit.

RoleProvider doesn't exist in .NET Core at least not in that form. I believe what you are looking for is the RoleManager . The implementation is quite similar to the below:

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

Kindly refer to this article for detail on how to implement it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM