efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Exception when trying to use the AddRoleManager

Open jvelezc opened this issue 10 years ago • 1 comments

Question information use case: i am trying to use ApplicationRoleManager in my constructor for a asp net 5 controller. My ApplicationRoleManager is basically concrete implementation of public class RoleManager : IDisposable where TRole : class Can somebody show me some example code of how to use //.AddRoleManager() instead of my work around.

Exception is: Asp Net 5 Exception: Exception: Additional information: Type ApplicationRoleManager must derive from RoleManager.

In my Startup.cs

  services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddUserManager<CustomUserManager>()
                //EXCEPTION OCCURS only when I try to AddRolemanager
                .AddRoleManager<ApplicationRoleManager>()
                .AddRoleStore<ApplicationRoleStore>()
                .AddDefaultTokenProviders();

My Model

 public class ApplicationRole : IdentityRole<string>
    {
        public ApplicationRole()
        {
            this.Id = Guid.NewGuid().ToString();
        }
        public ApplicationRole(string name)
           : this()
        {
            this.Name = name;
        }
    }

Predictably I try to use RoleManager where Application role derives from identity

public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(
            IRoleStore<ApplicationRole>store, 
            IEnumerable<IRoleValidator<ApplicationRole>> roleValidators,
            ILookupNormalizer keyNormalizer,
            IdentityErrorDescriber errors,
            ILogger<RoleManager<ApplicationRole>>  logger,
            IHttpContextAccessor contextAccessor
            )
           :base(
                    store:store,
                    roleValidators: roleValidators, 
                    keyNormalizer: keyNormalizer,
                    errors:errors,
                    contextAccessor: contextAccessor,
                    logger: logger)
        {
        }

    }

jvelezc avatar Dec 15 '15 23:12 jvelezc

I changed

services.AddIdentity<ApplicationUser, IdentityRole>()

to

services.AddIdentity<ApplicationUser, ApplicationRole>()

after noticing there was a type mismacth ApplicationRoleManager !

jvelezc avatar Dec 16 '15 00:12 jvelezc