Search code examples
identityserver4asp.net-core-identity

IUserClaimsPrincipalFactory`1 not registered error in IndentityServer implementation


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>();

Solution

  • 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.