efcore
efcore copied to clipboard
Exception when trying to use the AddRoleManager
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)
{
}
}
I changed
services.AddIdentity<ApplicationUser, IdentityRole>()
to
services.AddIdentity<ApplicationUser, ApplicationRole>()
after noticing there was a type mismacth ApplicationRoleManager !