'No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered
What is the possible cause of this error:
InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager [Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.
My target framework is netcoreapp2.1.
This is my user store class:
public class MyUserStore : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
And my user role class:
public class MyUserRole : IdentityRole
{
public string Description { get; set; }
}
My DbContext:
public class ApplicationDbContext : IdentityDbContext<MyUserStore,MyUserRole,string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>
options): base(options) { }
}
My ConfigureServices method in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//services.AddDefaultIdentity<IdentityUser>()
// .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<MyUserStore, MyUserRole>(cfg => {
cfg.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<ApplicationDbContext>();
services.AddTransient<Seeder>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
i want to understand why this is happening and what is the best practice.
Solution 1:[1]
This usually happens in the _LoginPartial.cshtml or _ManageNav.cshtml razor view. Eg.
@inject UserManager<IdentityUser> userManager
Must be changed to
@inject UserManager<MyUserStore> userManager
The same applies to SignInManager.
When registering your own MyUserStore (bad name, should be MyUser) for the AspNetCore Identity, the UserManager<> type will be registered to the ServiceCollection as UserManager<MyUserStore>.
Whenever you want to resolve the UserManager<>, specify the identity user model registered in your startup as the type parameter. Which would be UserManager<MyUserStore> in your specific case:
Or like-wise, when resolving it inside other classes, as may be the case in your Seeder service. The call stack of your exception should give you a hint of where this is happening.
Solution 2:[2]
In _LoginPartial.cshtml,
replace
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IndentityUser> UserManager
with
@using Microsoft.AspNetCore.Identity
@inject SignInManager<MyUserStore> SignInManager
@inject UserManager<MyUserStore> UserManager
Notice the difference, IdentityUser vs MyUserStore
Solution 3:[3]
Had same issue with core 2. One more area where you need to check is the file _ManageNav.cshtml. Try updating the line
@inject SignInManager<IdentityUser> SignInManager
with
@inject SignInManager<YOURCUSTOMMODEL> SignInManager
Solution 4:[4]
On the _ManageNav.cshtml file make sure you have the following:
@using PROJECTNAME.Models
@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
Please change PROJECTNAME to the name of your project.
Solution 5:[5]
Got same error in a partial view. Turns out if you are injecting sign in manager , you will have to register it in Configure Services (in Startup.cs/ in Program.cs in .Net 6)
In my case added this in ConfigureServices function in Startup.cs
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();
Solution 6:[6]
add below code in your Startup.cs or IdentityHostingStartup.cs file
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders().AddDefaultUI();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | James Graham |
| Solution 4 | Jorge Lobo |
| Solution 5 | |
| Solution 6 | Rokive |
