Search code examples
c#asp.net-coreidentityserver4asp.net-identity-3

IdentityServer4 can't add asp net core identity with custom user store


I'm trying to use my custom IUserStore implementation with IdentityServer4 + Asp Net Core Identity, the steps that I followed are creating new 'IdentityServer with Asp Net Core Identity' aka is4aspid template after removing EntityFramework assets then my configure services look like

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        services.AddScoped<IIdentityUserRepository<ApplicationUser>, IdentityUserRepository>(); //<-- Repository that I used on CustomUserStore

        services.AddDefaultIdentity<ApplicationUser>()
            .AddUserStore<CustomUserStore<ApplicationUser>>() //<-- Add
            .AddDefaultTokenProviders();

        var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;

                // see https://identityserver4.readthedocs.io/en/latest/topics/resources.html
                options.EmitStaticAudienceClaim = true;
            })
            .AddInMemoryIdentityResources(Config.IdentityResources)
            .AddInMemoryApiScopes(Config.ApiScopes)
            .AddInMemoryClients(Config.Clients)
            .AddAspNetIdentity<ApplicationUser>();
    }

And the custom user store looks like

public class CustomUserStore<TUser> :
    IUserStore<TUser>,
    IUserLoginStore<TUser>,
    IUserRoleStore<TUser>,
    IUserClaimStore<TUser>,
    IUserPasswordStore<TUser>,
    IUserSecurityStampStore<TUser>,
    IUserEmailStore<TUser>,
    IUserLockoutStore<TUser>,
    IUserPhoneNumberStore<TUser>
    where TUser : ApplicationUser
{
    private readonly IIdentityUserRepository<TUser> _userRepository;

    public CustomUserStore(IIdentityUserRepository<TUser> userRepository)
    {
        _userRepository = userRepository;
    } //rest of the code hidden sake of brevity

Custom user store works well with default Asp Net Core identity template but in is4aspid template when I try to get rid of Entity Framework and put my custom store implementation, Login page returns 404 message when I try to access protected resource but aside from that, home page works properly, there is no error message or log aside message below

[13:03:04 Information] Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler
AuthenticationScheme: Identity.Application was challenged.

Also while these things happening there is no call to the Controllers or the CustomUserStore

Documentations that I used

Custom storage providers for ASP.NET Core Identity

IdentityServer4 Using ASP.NET Core Identity

Edit: ApplicationUser class is custom implementation as well without any inheritors unlike default ApplicationUser : IdentityUser comes with template itself


Solution

  • Problem was the AddDefaultIdentity itself because it not only adds Identity components also bunch of things included UI and I think problem caused because the UI components added along with AddDefaultIdentity so while I trying to use project's views it confused the framework, solution was using AddIdentity instead of AddDefaultIdentity so this solved the problem and now the system works seamessly.