Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>();
like image 728
Kishan Vaishnav Avatar asked Apr 23 '21 05:04

Kishan Vaishnav


People also ask

What happened to the identityserver iuserservice?

The IdentityServer IUserService that was used to integrate your user store is also gone, replaced with a new user store abstraction in the form of IProfileService and IResourceOwnerPasswordValidator . You must now implement user authentication yourself (and that’s a good thing).

What is the identity-based implementation for identityserver?

An ASP.NET Identity-based implementation is provided for managing the identity database for users of IdentityServer. This implementation implements the extensibility points in IdentityServer needed to load identity data for your users to emit claims into tokens. The repo for this support is located here and the NuGet package is here.

How to implement identityserver4 on ASP NET Core?

Implementing IdentityServer4 on ASP.NET Core and .NET Core 1 OpenID Connect Discovery Document. ... 2 Signing Credentials. ... 3 Clients, Resources and Users. ... 4 OAuth Functionality. ... 5 Protecting an API. ... 6 Adding a User Interface. ... 7 OpenID Connect. ... 8 Entity Framework Core. ... 9 ASP.NET Core Identity. ...

How many tables are there in identityserverquickstart?

Here is the list of tables that we have in the “IdentityServerQuickstart” database. Seven tables that start with the “AspNet” prefix are the ASP.NET Core Identity tables that hold user store (users, claims, roles, logins, and user tokens).


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.

like image 72
Kishan Vaishnav Avatar answered Sep 28 '22 19:09

Kishan Vaishnav


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();
like image 21
onets Avatar answered Sep 28 '22 19:09

onets