InvalidOperationException: No service for type Microsoft AspNetCore Identity UserManager 1[Microsoft AspNetCore identity IdentityUser] has been registered


Question: How do you solve for error that says "InvalidOperationException: No service for type

"Microsoft.AspNetCore.Identity.UserManager 1[Microsoft.AspNetCore.Identity.IdentityUser] has been registered"


 in Asp.Net 6 Web Application?



Login to See the Rest of the Answer

Answer:
Take a look into your project, if you moved files like ApplicationDbContext around you might get this error. Follow the instruction below to validate that all is checked to find out where the error might be coming from:


1. Determine or check if your project is using a different standalone DataLayer or ORM Application Library that might contain the Data Folder with Database Migrations files and Application Contexts. 

- If this is the case, then you cannot have two Application Contexts in one project. The solution will be to delete one, keep in mind to keep the one with the correct Interface injected into the IdentityDbContext, see the code below:

 public class ApplicationDbContext : IdentityDbContext<UserProfile>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

    }

2. Check to see if the Startup.cs or the Program.cs has correct Interfaces or Services registered around Identity. See the code below on "How to add Login Code into Asp.Net 6 Application":

builder.Services.AddIdentity<UserProfile, IdentityRole>(options => {

    options.SignIn.RequireConfirmedAccount = false;
    // Default Password settings.
    options.Password.RequireDigit = false;
    options.Password.RequireLowercase = false;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;
    options.Password.RequiredLength = 3;
    options.Password.RequiredUniqueChars = 0;


})
      .AddEntityFrameworkStores<ApplicationDbContext>()

      .AddDefaultTokenProviders().AddDefaultUI();

 


InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered

Edited Version 2

 Guys, the error might be coming from the _LoginPartial located in Views/Shared/_LoginPartial. There is a Service Injection in the Partial with IdentityUser class passed in as a type. This happens when you have a custom Identity Users class like UserProfile or something. Simply, change the IdentityUser to UserProfile or equivalent to the class that you have.





© 2024 - ErnesTech - Privacy
E-Commerce Return Policy