skip to Main Content

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


  1. Try to modify your _LoginPartial.cshtml, change :

    @inject SignInManager<IdentityUser> SignInManager
    @inject UserManager<IdentityUser> UserManager
    

    into:

    @using razorIdentity.Models;
    @inject SignInManager<CustomUser> SignInManager
    @inject UserManager<CustomUser> UserManager
    
    Login or Signup to reply.
  2. 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):

    1. What version of .NET are you using?

    Hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search