0

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

phantomcloak
  • 1
  • 1
  • 3

2 Answers2

0

I use code like this to add my own store to IdentityServer:

services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(_configuration["ConnectionString"]);
});

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();
Tore Nestenius
  • 16,431
  • 5
  • 30
  • 40
  • Thanks but I want to use my custom stores instead of entity framework's such as https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-custom-storage-providers?view=aspnetcore-5.0 – phantomcloak Feb 06 '21 at 13:32
  • Exactly what error do you get and on what URL? if you get a 404 then that is not a custom store issue, its something else in your configuration. – Tore Nestenius Feb 06 '21 at 14:09
  • when I try to access protected resource for example diagnostics page identityserver automatically routes to `/Identity/Account/Login?ReturnUrl=%2Fdiagnostics` and it only gives 404 there is no error message or log when I plug off the my custom store and use EF Core it starts to work again and I'm only modifying few lines of code in that template so I don't think it could be due to configuration – phantomcloak Feb 06 '21 at 14:33
  • I guess that page does not exist then, do you want to use the IdentityServer Account login page or the one provided by ASP.NET Identity? The pages for ASP.NET Identity is found inside the NuGet package library for Identity. – Tore Nestenius Feb 06 '21 at 15:30
  • I want to use page that template supplies and so page is exist, problem itself is strange because as I said I'm using template not my code, everything included pages working seamlessly until I add `AddUserStore>()` after prompt page (the page asks for username and password) returns 404 I think it could be middle ware problem but I have no logs no error messages or single clue – phantomcloak Feb 06 '21 at 15:50
  • You can always set the debug level to debug for the related namespaces to get a better insight into why it fails. – Tore Nestenius Feb 06 '21 at 16:43
0

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.

phantomcloak
  • 1
  • 1
  • 3