ASP.NET Core Identity: No service for role manager
Solution 1
What am I doing wrong? My gut says there's something wrong with how I add the RoleManager as a service.
The registration part is actually fine, tho' you should remove services.AddScoped<RoleManager<IdentityRole>>()
, as the role manager is already added for you by services.AddIdentity()
.
Your issue is most likely caused by a generic type mismatch: while you call services.AddIdentity()
with IdentityRole<int>
, you try to resolve RoleManager
with IdentityRole
, which is an equivalent of IdentityRole<string>
(string
being the default key type in ASP.NET Core Identity).
Update your Configure
method to take a RoleManager<IdentityRole<int>>
parameter and it should work.
Solution 2
I was having this issue
No service for type 'Microsoft.AspNetCore.Identity.RoleManager`
And this page was the first result on Google. It did not answer my question, so I thought I would put my solution here, for anyone else that may be having this problem.
ASP.NET Core 2.2
The missing line for me was .AddRoles() in the Startup.cs file.
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
Hope this helps someone
Source: https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-2.2 (at the bottom)
Related videos on Youtube
Glenn Utter
Updated on April 06, 2020Comments
-
Glenn Utter about 4 years
I have an ASP.NET Core app that uses Identity. It works, but when I am trying to add custom roles to the database I run into problems.
In Startup
ConfigureServices
I have added Identity and the role manager as a scoped service like this:services.AddIdentity<Entities.DB.User, IdentityRole<int>>() .AddEntityFrameworkStores<MyDBContext, int>(); services.AddScoped<RoleManager<IdentityRole>>();
and in Startup
Configure
I inject RoleManager and pass it to my custom classRolesData
:public void Configure( IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, RoleManager<IdentityRole> roleManager ) { app.UseIdentity(); RolesData.SeedRoles(roleManager).Wait(); app.UseMvc();
This is the
RolesData
class:public static class RolesData { private static readonly string[] roles = new[] { "role1", "role2", "role3" }; public static async Task SeedRoles(RoleManager<IdentityRole> roleManager) { foreach (var role in roles) { if (!await roleManager.RoleExistsAsync(role)) { var create = await roleManager.CreateAsync(new IdentityRole(role)); if (!create.Succeeded) { throw new Exception("Failed to create role"); } } } } }
The app builds without errors, but when trying to access it I get the following error:
Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore`1[Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager
What am I doing wrong? My gut says there's something wrong with how I add the RoleManager as a service.
PS: I have used "No authentication" when creating the project to learn Identity from scratch.
-
RussHooker almost 7 yearsThis was a great fix and very helpful. I also recommend calling the SeedRoles from a DatabaseInitializer so that does the CreateUsers and the roles async from the startup something like
code
public async Task SeedAsync(){ await CreateUsersAsync(); await RolesData.SeedRoles(_roleManager);} -
Carlos Garcia about 5 yearsAnd what about the UserManager?
-
Eric K almost 5 yearsThis method produced errors for me. I changed the
IdentityRole<Guid>
toIdentityRole
throughout and it worked like a charm. -
Dave ت Maher over 4 yearsI was building an internal app for a company, so I'm using active directory to authorize the user and get the roles
-
Minasie Shibeshi over 3 yearsI had a function where I seed some data, including roles. And when I fetched the RoleManager service, I did serviceProvider.GetRequiredService<RoleManager<IdentityRole>>(); it should have been serviceProvider.GetRequiredService<RoleManager<IdentityRole<int>>>(); with the generic integer specificity