Multiple Identities in ASP.NET Core 2.0

后端 未结 3 589
眼角桃花
眼角桃花 2020-12-08 16:38

I am migrating an ASP.NET Core 1.0 application to ASP.NET Core 2.0.

In my startup I am configuring two identities:

services.AddIdentity

        
3条回答
  •  一整个雨季
    2020-12-08 17:29

    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(
                this IServiceCollection services)
                where TUser : class
                where TRole : class
            {
                services.TryAddScoped, UserValidator>();
                services.TryAddScoped, PasswordValidator>();
                services.TryAddScoped, PasswordHasher>();
                services.TryAddScoped, RoleValidator>();
                services.TryAddScoped>();
                services.TryAddScoped, UserClaimsPrincipalFactory>();
                services.TryAddScoped, AspNetUserManager>();
                services.TryAddScoped, SignInManager>();
                services.TryAddScoped, AspNetRoleManager>();
    
                return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
            }
        }
    }
    

提交回复
热议问题