Multiple Identities in ASP.NET Core 2.0

后端 未结 3 598
眼角桃花
眼角桃花 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:36

    Asp.net Core 2.2 provides a built-in method for that purpose.

    AddIdentityCore
    

    How to use it:

    services.AddIdentity(configureIdentity)
       .AddDefaultTokenProviders()
       .AddUserStore>()
       .AddRoleStore>();
    
    services.AddIdentityCore(configureIdentity)
       .AddDefaultTokenProviders()
       .AddErrorDescriber()
       .AddUserStore>()
       .AddRoleStore>();
    
    services.AddScoped>();
    

    In fact, read the implementation of this method from asp.net core 2.2 github repo

        /// 
        /// Adds and configures the identity system for the specified User type. Role services are not added by default 
        /// but can be added with .
        /// 
        /// The type representing a User in the system.
        /// The services available in the application.
        /// An action to configure the .
        /// An  for creating and configuring the identity system.
        public static IdentityBuilder AddIdentityCore(this IServiceCollection services, Action setupAction)
            where TUser : class
        {
            // Services identity depends on
            services.AddOptions().AddLogging();
    
            // Services used by identity
            services.TryAddScoped, UserValidator>();
            services.TryAddScoped, PasswordValidator>();
            services.TryAddScoped, PasswordHasher>();
            services.TryAddScoped();
            services.TryAddScoped, DefaultUserConfirmation>();
            // No interface for the error describer so we can add errors without rev'ing the interface
            services.TryAddScoped();
            services.TryAddScoped, UserClaimsPrincipalFactory>();
            services.TryAddScoped>();
    
            if (setupAction != null)
            {
                services.Configure(setupAction);
            }
    
            return new IdentityBuilder(typeof(TUser), services);
        }
    

提交回复
热议问题