【问题标题】:How to add more than one identity store in aspnet core identity? [duplicate]如何在 aspnet 核心身份中添加多个身份存储? [复制]
【发布时间】:2019-05-14 04:31:39
【问题描述】:

我目前正在使用 IdentityServer4 添加 ASP.NET Core Identity。

我添加了类似于下面一行的身份。

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

我想要一个类似的存储库,所以我创建了一组单独的内部对象,例如 InternalApplicationDbContext、InternalIdentityUser。我认为这就像配置上述步骤并注入它一样简单......

UserManager<InternalIdentityUser>

但是,它似乎不起作用,并且我收到与此类似的错误。 方案已存在 https://github.com/aspnet/AspNetCore.Docs/issues/8223

我已经阅读了一些与此相关的文档,但没有任何迹象表明它支持多个身份提供者。是这种情况还是我错过了什么?

总而言之,我想要两个独立的数据库来管理用户,一个用于公共用户,另一个用于内部用户。我想使用内置的身份 API UserManager 来封装实现,因为我真的不想构建自己的。

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

【问题讨论】:

    标签: c# .net-core asp.net-identity identityserver4


    【解决方案1】:

    在 github 上查看 ASP.NET Core 源代码后,可以使用此扩展方法添加第二个身份:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2019-03-24
      • 2018-05-12
      • 1970-01-01
      相关资源
      最近更新 更多