1

I'm currently adding ASP.NET Core Identity with IdentityServer4.

I've added the identity similar to the line below.

services.AddDbContext<PublicApplicationDbContext>(options =>
                options.UseSqlServer(
                    connection))
                .AddIdentity<PublicIdentityUser, PublicIdentityRole>(opts =>
                {
                    opts.Password.RequiredLength = 6;
                })
                .AddDefaultTokenProviders()
                .AddEntityFrameworkStores<PublicApplicationDbContext>();

I wanted to a similar repository so I created a separate set of internal objects like InternalApplicationDbContext, InternalIdentityUser. I thought it would be as easy as configuring the steps above and injecting this...

UserManager<InternalIdentityUser>

However, it doesn't seem to work and I get an error similar to this. Scheme already exists https://github.com/aspnet/AspNetCore.Docs/issues/8223

I've read some documentation related to this but nothing implies it supports more than one identity provider. Is this the case or am I missing something?

To summarize, I want two separate databases to manage users, one for public users and another for internal ones. I want to use the built-in identity API UserManager to encapsulate the implementation as I honestly don't want to build my own.

https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-custom-storage-providers?view=aspnetcore-2.2#references

MichaelChan
  • 1,808
  • 17
  • 34

1 Answers1

1

After looking through the ASP.NET Core source code on github, a second identity could be added using this extension method:

using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Text;

namespace Whatever
{
    public static class IdentityExtensions
    {
        public static IdentityBuilder AddSecondIdentity<TUser, TRole>(
            this IServiceCollection services)
            where TUser : class
            where TRole : class
        {
            services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
            services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
            services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
            services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
            services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
            services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
            services.TryAddScoped<UserManager<TUser>, AspNetUserManager<TUser>>();
            services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
            services.TryAddScoped<RoleManager<TRole>, AspNetRoleManager<TRole>>();

            return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
        }
    }
}

https://stackoverflow.com/a/47434573/8006943

evilGenius
  • 1,041
  • 1
  • 7
  • 16