I would like to register my service using the CustomUser class, which inherits from IdentityUser.
However, when I do I get the following error in my browser:
InvalidOperationException: No service for type ‘Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]’ has been registered.
My relevant code:
builder.Services.AddDefaultIdentity<CustomUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
using Microsoft.AspNetCore.Identity;
namespace WebApplication3.Models
{
public class CustomUser : IdentityUser
{
public String Email { get; set; }
public String Password { get; set; }
}
}
2
Answers
Try to modify your _LoginPartial.cshtml, change :
into:
Based on what you have provided, including the code, the problem seems to be relagted to the registration of the UserManager service for the CustomUser class.
By default, AddDefaultIdentity expects the TUser to be of type IdentityUser, and when you try to use a custom user class, in this case, **Customer*User, the UserManager service is not automatically registered for it.
I think that the solution is in the following Microsoft article:
Identity model customization in ASP.NET Core
The above link should be a good starting point.
Question(s):
Hope this helps.