Invalidoperationexception: no service for type ‘microsoft.aspnetcore.identity.usermanager`1[microsoft.aspnetcore.identity.identityuser]’ has been registered.

The InvalidOperationException error occurs when the ASP.NET Core Identity UserManager is not registered as a service in the application’s dependency injection container. This typically happens when the necessary services for ASP.NET Core Identity are not configured correctly.

To resolve this error, you need to make sure that you have registered the appropriate services for ASP.NET Core Identity in your application’s startup code.

Here’s an example of how to register the ASP.NET Core Identity services in the ConfigureServices method of the Startup class:


  public void ConfigureServices(IServiceCollection services)
  {
      // Other service registrations...

      services.AddDbContext<YourDbContextClass>(options =>
          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

      services.AddDefaultIdentity<IdentityUser>()
          .AddEntityFrameworkStores<YourDbContextClass>();

      // Other service configurations...
  }
  

In the above example, we first add the database context using AddDbContext method, specifying the connection string. Then, we use AddDefaultIdentity with IdentityUser as the type argument to register the default ASP.NET Core Identity services with the provided user class. Make sure to replace YourDbContextClass with your actual DbContext class.

By registering the services correctly, the InvalidOperationException should no longer occur.

Read more

Leave a comment