IUserClaimsPrincipalFactory`1 not registered error in IndentityServer implementation

Viewed 2754

I am getting below exception even after registering the AddDefaultIdentity

System.InvalidOperationException: Service type: IUserClaimsPrincipalFactory`1 not registered.

Here's the Identity registration code:

services.AddDefaultIdentity<ApplicationUser>(options =>
            {
                //Disable account confirmation.
                options.SignIn.RequireConfirmedAccount = false;
                options.SignIn.RequireConfirmedEmail = false;
                options.SignIn.RequireConfirmedPhoneNumber = false;
            })
            .AddEntityFrameworkStores<ApplicationDbContext>();

And here's the IdentityServer code:

        var builder = services.AddIdentityServer()
            .AddInMemoryApiScopes(IdentityServerConfig.ApiScopes)
            .AddInMemoryIdentityResources(IdentityServerConfig.IdentityResources)
            .AddInMemoryApiResources(IdentityServerConfig.ApiResources)
            .AddInMemoryClients(IdentityServerConfig.Clients)
            .AddAspNetIdentity<IdentityUser>();
2 Answers

I spent hours solving this typo.

The problem was that my registration was wrong.

I was working with ApplicationUser but I was registering IdentityUser in the identity server.

I fixed my problem by setting the ApplicationUser in both AddIdentityServer() and AddDefaultIdentity().

Here's the working code: .Net Core Identity

services.AddDefaultIdentity<ApplicationUser>(options =>
            {
                //Disable account confirmation.
                options.SignIn.RequireConfirmedAccount = false;
                options.SignIn.RequireConfirmedEmail = false;
                options.SignIn.RequireConfirmedPhoneNumber = false;
            })
            .AddEntityFrameworkStores<ApplicationDbContext>();

IdentityServer4:

var builder = services.AddIdentityServer()
                .AddInMemoryApiScopes(IdentityServerConfig.ApiScopes)
                .AddInMemoryIdentityResources(IdentityServerConfig.IdentityResources)
                .AddInMemoryApiResources(IdentityServerConfig.ApiResources)
                .AddInMemoryClients(IdentityServerConfig.Clients)
                .AddAspNetIdentity<ApplicationUser>();

Notice the use of ApplicationUser. I posted question here so that I remember it next time.

I fixed this error by changing the order. I had

var builder = services.AddIdentityServer()
    .AddInMemoryIdentityResources(Config.IdentityResources)
    .AddInMemoryClients(Config.Clients)
    .AddAspNetIdentity<ApplicationUser>();

builder.AddDeveloperSigningCredential();

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySql(Configuration.GetConnectionString("ApplicationDbContext")));

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

Now I have:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySql(Configuration.GetConnectionString("ApplicationDbContext")));

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

var builder = services.AddIdentityServer()
    .AddInMemoryIdentityResources(Config.IdentityResources)
    .AddInMemoryClients(Config.Clients)
    .AddAspNetIdentity<ApplicationUser>();

builder.AddDeveloperSigningCredential();
Related