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?


Answer: Take a look into the 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. Determin 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();

 


Edit this Article
UserManager<IdentityUser> How to add Identity to AspNet 6 app AspNet 6 Login ebook e-book .NET 6 published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy