If you are working on an ASP.NET Core application, you might encountered this error that happen when you launch your application:

No service for type ‘Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]’ has been registered

Here is a screenshot of the error:

This error can be thrown by ASP.NET Core when you are using ASP.NET Identity with a custom IdentityUser class. Indeed, by default, ASP.NET Core Identity use a IdentityUser<string> type to define the type of the primary key used for its schema. As I’m not a big fan of having GUID stored as string, I like to define my own class:

public class User : IdentityUser<Guid>
{
}

Then, on startup, I simply tell the runtime that I want to use my base class and not the one from the Framework:

services.AddIdentity<User, Role>()
    .AddDefaultUI(UIFramework.Bootstrap4)
    .AddEntityFrameworkStores<MyDbDbContext>()
    .AddDefaultTokenProviders();

This code is working well but you need to remember that when using the Identity feature, the runtime will need to reuse this classes to determine if user is logged in or not, etc.
Here is the code relevant for that in the file “_LoginPartial.cshtml“:

So simply replace “IdentityUser” by your own custom class and everything will work correctly:

Happy coding! 🙂


Docker, SQL Server and the "Exited (1)" status How to remove tick marks in Chart.JS

Leave a Reply

Your email address will not be published. Required fields are marked *