• asp.net core 3.x Identity


    一、前言

    这方面的资料很多,重复的写没必要,但是最近一直在学习身份验证和授权相关东东,为了成体系还是写一篇,主要是从概念上理解identity系统。

    参考:https://www.cnblogs.com/r01cn/p/5179506.html

    二、概述

    几乎所有系统都包含用户、角色、权限、登录、注册等等,以前我们通常是自己来实现,定义各种实体、以及对应的Repository、Service类,其实这些功能早在.net 2.0就已有实现,并且一直在进化,要么是因为大家不了解,要么是因为觉得扩展性不强,最终使用微软提供的用户管理的人不多,这里就不扯以前的事了。现在的asp.net core延续了.net framework最后一次实现的Identity,暂且称为“标识系统”吧。我的理解是它主要提供用户管理、角色管理等功能,并且提供了相关的类与身份验证系统结合,还包括持久化和UI,还提供了很多扩展点,当然预留了很多扩展点,也提供了默认实现,就使用起来。用还是不用了解之后再做决定也不迟。

    身份验证、授权、用户/角色管理这三个系统是独立的,只是授权依赖身份验证,你要判断某人能干啥,至少得让他先登录吧。所以identity系统不是必须的,但是用它比自己实现更方便。另一个原因一些开源框架对于用户、角色、权限、登录这些东西往往也是直接使用的微软原生的,只是做了些许扩展和集成。

    主要概念包括:

    • 用户实体:IdentityUser,对应到用户数据表
    • 用户数据操作接口:UserStore,对用户进行CRUD和其它功能的  与数据库操作相关的功能
    • 用户管理器:UserManager,对用户进行CRUD和相关操作的业务逻辑类,
    • 登录器:SignInManager提供登录相关功能,实现identity系统和身份验证系统的结合
    • 角色实体:类别用户
    • 角色数据操作接口:类别用户
    • 角色管理器:类比用户

    默认使用EF做数据库相关操作,当然非常容易扩展。

    三、IdentityUser

    系统中通常有两种用户,

    • 一个是asp.net框架中的当前登录用户,也就是HttpContext.User,它是ClaimsPrincipal类型。它里面相对来说属性比较少,只包含用户id和与授权判断相关数据,比如角色
    • 一个是我们做用户管理时的用户实体。它包含完整的用户信息

    比如像“手机号”这个字段在用户实体里肯定是有的,但是没必要放进当前登录用户,因为这个字段不是每次请求都需要的。只要那些被平凡访问的字段放进当前登录用户才比较合适。

    这个用户就是用户实体,对应到数据库的用户表。用下半身也能想到它大概包含哪些属性:id、姓名、账号、密码、邮箱、性别、出生年月、民族、籍贯、地址、电话、巴拉巴拉.....

    大概了解了啥是用户,我们来看看它比较特殊的几个点

    3.1、继承结构

     public class IdentityUser<TKey>    public class IdentityUser : IdentityUser<string> 

    因为在设计identity系统时,不晓得将来的开发人员希望用什么类型作为主键,所以定义成泛型。又由于大部分时候可能使用字符串(可能是对应guid),默认有个实现。

    3.2、特殊字段

    NormalizedUserName:标准化的用户名,默认是直接将UserName属性转换为大写,主要可能用来方便提升查询性能的。类似的还有Email与NormalizedEmail的关系
    EmailConfirmed:是否已做了邮箱确认。我们注册用户时会向用户发送一封邮件,用户进入邮箱点击邮件中的地址进行激活,此字典就是标识是否已激活了,类似的还有手机号确认PhoneNumberConfirmed
    SecurityStamp:当在后台修改用户的跟安全相关的信息,比如修改了密码,更新用户到数据库会自动更新此值,这样用户下次登录时可以判断此值变化了,要求用户重新登录。
    ConcurrencyStamp:跟上面有点类似,不过主要是做并发冲突的,是针对整个用户所有字段更新的,比如有两个现成都在修改同一个用户,会比对此字段是否另一个用户也在修改,如果是则其中一个线程修改失败
    AccessFailedCount:登录失败次数,多次登录失败会锁定的功能会用到此字段。

    3.3、额外概念

    UserClaims:当用户做第三方登录(比如:微信、google)时,可能会获取第三方中关于用户的额外字段,这些字段就是存储在用户的Claim中的,所以这是一个一对多关系,一个用户拥有多个第三方Claim
    UserLogins:用户第三方登录,记录某用户绑定的第三方登录的信息,比如openid啥的,所以也是一对多关系,一个用户可能绑定多个第三方登录
    UserTokens:用户登录 发送手机验证码、或邮箱激活用的验证码之类的,也是一对多

    3.x、如何扩展

    定义IdentityUser子类、定义UserManager子类、定义RoleManager子类 ,由于这些还没说,所以后面讲吧

    四、跟用户相关的数据操作接口Store

     定义一堆接口来对用户进行管理,这些功能都是跟数据库操作相关的。看看这些接口是干啥用的:

    • IUserLoginStore:管理用户绑定第三方登录的数据操作相关功能,比如获取绑定的第三方登录列表
    • IUserClaimStore:用户做第三方登录获取的用户的第三方账号相关字段值
    • IUserPasswordStore:专门针对用户密码的处理,比如设置获取某用户的密码
    • IUserSecurityStampStore:参考
    • IUserEmailStore:针对用户邮箱地址的处理
    • 略....

    为毛要分开定义呢?还是那个话你可以对单独的功能进行扩展。

    可以看到整个继承体系使用了很多泛型,目的是方便我们将来进行扩展,比如:我们将来可能使用int作为用户的主键、也可能自定义一个用户子类扩展更多字段、也可能继承Role实现更多属性、也可能不使用EF,也可能使用EF,但是希望用自己的DBContext等等。。。。

    为了将来扩展时尽量写更少的代码,所以实现了抽象类,也提供了默认子类,所以将来如果没有特殊需求,对用户管理的数据操作就完全不用自己写,有特殊需求是简单实现一个子类就ok拉,最最最复杂的情况我们才需要自己来实现这个接口

    默认情况下使用UserStore这个类,它实现上面说的所有接口。

    五、UserManager

    用户管理的业务逻辑类

    内部使用IUserStore

    最奇葩的是内部针对数据库操作的上面说了分开接口定义的,但是在这个类内部使用时只注入了IUserStore,然后对特殊步骤的处理是直接拿这个强转的。。所以将来扩展时我们必须重写单独那个方法,而不是只替换Store

    AspNetUserManager<TUser>子类提供identity框架与asp.net框架结合的,主要就体现当前请求的CancellationToken的处理

    待补充...........

    六、SignInManager

    用来结合identity系统和 身份验证系统的,主要提供登录、注销相关功能。登录又分为多种:账号密码登录、第三方登录、双因素登录

    七、启动配置

    7.1、AddDefaultIdentity

    1. 注册身份验证需要的核心服务;设置默认身份验证方案为“Identity.Appliction”,设置默认登录使用的身份验证方案为"Identity.External"。
    2. 注册多个身份验证验证方案,其中就包含我们最常用的基于cookie的身份验证方案以及第三方登录、双因素登录等对应的身份验证方案
    3. 注册标识系统跟用户管理相关的各种服务(这些服务主要在UserManager中被使用)
     1 1public static IdentityBuilder AddDefaultIdentity<TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions) where TUser : class
     2 {
     3      services.AddAuthentication(o =>
     4      {
     5            o.DefaultScheme = IdentityConstants.ApplicationScheme;
     6            o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
     7      })
     8      .AddIdentityCookies(o => { });
     9 
    10      return services.AddIdentityCore<TUser>(o =>
    11      {
    12            o.Stores.MaxLengthForKeys = 128;
    13            configureOptions?.Invoke(o);
    14      })
    15      .AddDefaultUI()
    16      .AddDefaultTokenProviders();
    17 }
    AddDefaultIdentity

    7.2、AddIdentityCookies

    这里对应上面的步骤2,上面源码第8行

    添加多个基于基于cookie的身份验证方案,具体如下:

    1. 名称=“Identity.Appliction”;  选项类型=CookieAuthenticationOptions;  身份验证处理器类型=CookieAuthenticationHandler
    2. 名称=Identity.External”;      选项类型=CookieAuthenticationOptions;  身份验证处理器类型=CookieAuthenticationHandler
    3. 名称=“Identity.TwoFactorRememberMe”;    选项类型=CookieAuthenticationOptions;  身份验证处理器类型=CookieAuthenticationHandler
    4. 名称=“Identity.TwoFactorUserId”;      选项类型=CookieAuthenticationOptions;  身份验证处理器类型=CookieAuthenticationHandler

    可以看到这4个身份验证方案的身份验证处理器和选项对象的类型都是一样的,只是方案名称不同,当然选项的值也不同。
    第1个身份验证方案是我们最常用的基于cookie的身份验证,且它是默认身份验证方案,意思是将来请求抵达时身份验证中间件将尝试从cookie中获取用户标识。
    第2个身份验证方案是跟第三方登录相关的,第3、4个是跟双因素登录(估计类似手机验证码登录)相关的,后期再说。 

     1 public static class IdentityCookieAuthenticationBuilderExtensions
     2 {
     3         public static IdentityCookiesBuilder AddIdentityCookies(this AuthenticationBuilder builder)
     4             => builder.AddIdentityCookies(o => { });
     5 
     6         public static IdentityCookiesBuilder AddIdentityCookies(this AuthenticationBuilder builder, Action<IdentityCookiesBuilder> configureCookies)
     7         {
     8             var cookieBuilder = new IdentityCookiesBuilder();
     9             cookieBuilder.ApplicationCookie = builder.AddApplicationCookie();
    10             cookieBuilder.ExternalCookie = builder.AddExternalCookie();
    11             cookieBuilder.TwoFactorRememberMeCookie = builder.AddTwoFactorRememberMeCookie();
    12             cookieBuilder.TwoFactorUserIdCookie = builder.AddTwoFactorUserIdCookie();
    13             configureCookies?.Invoke(cookieBuilder);
    14             return cookieBuilder;
    15         }
    16 
    17         public static OptionsBuilder<CookieAuthenticationOptions> AddApplicationCookie(this AuthenticationBuilder builder)
    18         {
    19             builder.AddCookie(IdentityConstants.ApplicationScheme, o =>
    20             {
    21                 o.LoginPath = new PathString("/Account/Login");
    22                 o.Events = new CookieAuthenticationEvents
    23                 {
    24                     OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
    25                 };
    26             });
    27             return new OptionsBuilder<CookieAuthenticationOptions>(builder.Services, IdentityConstants.ApplicationScheme);
    28         }
    29 
    30         public static OptionsBuilder<CookieAuthenticationOptions> AddExternalCookie(this AuthenticationBuilder builder)
    31         {
    32             builder.AddCookie(IdentityConstants.ExternalScheme, o =>
    33             {
    34                 o.Cookie.Name = IdentityConstants.ExternalScheme;
    35                 o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
    36             });
    37             return new OptionsBuilder<CookieAuthenticationOptions>(builder.Services, IdentityConstants.ExternalScheme);
    38         }
    39 
    40         public static OptionsBuilder<CookieAuthenticationOptions> AddTwoFactorRememberMeCookie(this AuthenticationBuilder builder)
    41         {
    42             builder.AddCookie(IdentityConstants.TwoFactorRememberMeScheme, o => o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme);
    43             return new OptionsBuilder<CookieAuthenticationOptions>(builder.Services, IdentityConstants.TwoFactorRememberMeScheme);
    44         }
    45 
    46         public static OptionsBuilder<CookieAuthenticationOptions> AddTwoFactorUserIdCookie(this AuthenticationBuilder builder)
    47         {
    48             builder.AddCookie(IdentityConstants.TwoFactorUserIdScheme, o =>
    49             {
    50                 o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
    51                 o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
    52             });
    53             return new OptionsBuilder<CookieAuthenticationOptions>(builder.Services, IdentityConstants.TwoFactorUserIdScheme);
    54         }
    55     }
    View Code

    7.2.1、OptionsBuilder<TOptions>

    若要完全掌握asp.net core的选项模型请参考:https://www.cnblogs.com/artech/p/inside-asp-net-core-06-01.html

    通常我们定义一样选项模型的赋值逻辑是在Startup中通过如下方式来定义

     1 public void ConfigureServices(IServiceCollection services)
     2 {
     3        services.Configure<CookiePolicyOptions>(opt => {
     4               //选项对象的赋值逻辑
     5               //opt...
     6        });
     7        services.PostConfigure<CookiePolicyOptions>(opt => {
     8               //选项对象的覆盖逻辑
     9               //opt...
    10        });

    若我们开发一个功能组件,可以调用方一个机会可以对我们组件进行配置,通常我们提供一个选项对象,调用方可以通过上面的方式来定义选项对象的赋值逻辑,我们组件内部通过依赖注入直接使用这个选项对象就可以了。更好的方式是我们的组件向外暴露一个OptionsBuilder<TOptions>,其中TOptions就是选项类型。下面直接通过如何使用它来理解它的用途。假设我们的组件向调用方暴露一个OptionsBuilder<MyModule>

    1 var optBuilder = //我们的模块提供一个扩展方法返回OptionsBuilder<MyModuleOptions>
    2 optBuilder.Configre(opt=>{
    3      opt.XXXX = xxx..;
    4      //.....
    5 });

    当然也提供了PostConfigure,可以发现返回的对象已经指明了选项对象的类型,因此后续的调用无需提供泛型参数,所以调用方会觉得这种方式更简单;而且此Builder对象是专门针对我们的模块的选项对象,所以更具相关性。

    OptionsBuilder<TOptions>是一个通用类,所不同功能模块的不同选项对象都可以使用它。

    7.2.2、IdentityCookiesBuilder

    1 public class IdentityCookiesBuilder
    2 {
    3     public OptionsBuilder<CookieAuthenticationOptions> ApplicationCookie { get; set; }
    4     public OptionsBuilder<CookieAuthenticationOptions> ExternalCookie { get; set; }
    5     public OptionsBuilder<CookieAuthenticationOptions> TwoFactorRememberMeCookie { get; set; }
    6     public OptionsBuilder<CookieAuthenticationOptions> TwoFactorUserIdCookie { get; set; }
    7 }

    7.2.3、如何为每种身份验证方案设置选项

    添加一个身份验证方案通常需要:设置方案名、指定身份验证处理器类型、配置身份验证选项对象。根据上面的说明及源码我们发现AddDefaultIdentity中的AddIdentityCookies只是添加了4个身份验证方案,分别设置了名称,并因为AddCookie,所以都是添加的CookieAuthenticationHandler作为身份验证处理器的,那么选项对象如何配置呢?其实就是调用AddIdentityCookies丢入一个委托进行配置的,只是AddDefaultIdentity中没有给了一个空委托 .AddIdentityCookies(o => { }); ,完全可以通过类似如下代码进行配置

     1 .AddIdentityCookies(o => {
     2     //配置默认身份验证的选项 
     3     o.ApplicationCookie.Configre(opt=>{}
     4         opt.....//配置
     5     );
     6     //配置第三方登录的相关选项
     7     o.ExternalCookie.Configre(opt=>{}
     8         opt.....//配置
     9     );
    10     ....双因素登录...
    11 });

    遗憾的是AddDefaultIdentity没有给我机会来配置这些选项。可能之所以叫AddDefault的原因,其实安全可以在参数中多提供一个委托,内部做下回调,从而允许我们可以进行配置。话说回来我们完全可以不使用AddDefaultIdentity,而是手动去Startup中注册相关服务并配置选项。类似下面这样

     1 public void ConfigureServices(IServiceCollection services)
     2 {
     3             services.AddDbContext<ApplicationDbContext>(options =>
     4                 options.UseSqlServer(
     5                     Configuration.GetConnectionString("DefaultConnection")));
     6 
     7             services.AddAuthentication(o =>
     8             {
     9                 o.DefaultScheme = IdentityConstants.ApplicationScheme;
    10                 o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
    11             }).AddApplicationCookie().Configure(opt=> { 
    12                 //配置此方案的选项
    13             });
    14             services.AddIdentityCore<IdentityUser>(o =>
    15             {
    16                 o.Stores.MaxLengthForKeys = 128;
    17             })
    18                .AddDefaultUI()
    19                .AddDefaultTokenProviders()
    20                .AddEntityFrameworkStores<ApplicationDbContext>();
    View Code

     另外如果我们不配置则这些选项对象将使用默认值,参考:asp.net core 3.x 身份验证-2启动阶段的配置

    7.3、AddIdentityCore

    对应总步骤3,注册Identity(标识系统)跟用户管理相关的服务。具体参考下面的源码:

     1 public static IdentityBuilder AddIdentityCore<TUser>(this IServiceCollection services, Action<IdentityOptions> setupAction) where TUser : class
     2 {
     3      // Services identity depends on
     4      services.AddOptions().AddLogging();
     5 
     6      // Services used by identity
     7      services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
     8      services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
     9      services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
    10      services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
    11      services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
    12      // No interface for the error describer so we can add errors without rev'ing the interface
    13      services.TryAddScoped<IdentityErrorDescriber>();
    14      services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser>>();
    15      services.TryAddScoped<UserManager<TUser>>();
    16      if (setupAction != null)
    17      {
    18           services.Configure(setupAction);
    19      }
    20      return new IdentityBuilder(typeof(TUser), services);
    21 }
    View Code

    7.3.1、IdentityOptions

    Identity系统中会用到各种配置选项就是通过它来提供的,它相当于一个选项对象的容器,里面包含各种子选项对象,看看源码

     1 public class IdentityOptions
     2 {
     3     public ClaimsIdentityOptions ClaimsIdentity { get; set; } = new ClaimsIdentityOptions();
     4     public UserOptions User { get; set; } = new UserOptions();
     5     public PasswordOptions Password { get; set; } = new PasswordOptions();
     6     public LockoutOptions Lockout { get; set; } = new LockoutOptions();
     7     public SignInOptions SignIn { get; set; } = new SignInOptions();
     8     public TokenOptions Tokens { get; set; } = new TokenOptions();
     9     public StoreOptions Stores { get; set; } = new StoreOptions();
    10 }
    View Code

    这个选项对象的赋值逻辑是通过AddDefaultIdentity的委托参数传进来的,这意味着我们配置时可以通过如下方式来配置Identity系统的选项:

    1 services.AddDefaultIdentity<IdentityUser>(options =>
    2 {
    3       options.SignIn.RequireConfirmedAccount = true;
    4       options.Password.RequiredLength = 9;//密码相关配置
    5       options.SignIn.RequireConfirmedEmail = true;//登录相关配置
    6       //其它相关配置....
    7 })
    8 .AddEntityFrameworkStores<ApplicationDbContext>();

    在登录管理器SignInManager、UserManager等多个组件都会使用到此选项对象。

    7.3.2、IdentityBuilder

    AddIdentityCore方法只是注册与用户管理相关服务,这是一种最小注册,如果还希望使用更多Identity系统提供的功能还需要注册其它相关服务,注册这些服务的方法就定义在IdentityBuilder,其实完全可以使用services进行注册,只是使用IdentityBuilder更简单,也跟具相关性,因为这些注册方法都是跟Identity系统相关的。

    AddIdentityCore的返回类型是IdentityBuilder、也作为AddDefaultIdentity的最终返回对象。因此我们可以在调用AddDefaultIdentity后继续做其它服务的配置,例如在Startup中这样:

    services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddRoleManager<RoleManager<IdentityUser>>()//注册角色管理器
                .AddEntityFrameworkStores<ApplicationDbContext>();//注册基于ef的数据操作类

    IdentityBuilder完整源码:

      1 public class IdentityBuilder
      2 {
      3     public IdentityBuilder(Type user, IServiceCollection services)
      4     {
      5         UserType = user;
      6         Services = services;
      7     }
      8     public IdentityBuilder(Type user, Type role, IServiceCollection services) : this(user, services)
      9         => RoleType = role;
     10     public Type UserType { get; private set; }
     11     public Type RoleType { get; private set; }
     12     public IServiceCollection Services { get; private set; }
     13     private IdentityBuilder AddScoped(Type serviceType, Type concreteType)
     14     {
     15         Services.AddScoped(serviceType, concreteType);
     16         return this;
     17     }
     18     public virtual IdentityBuilder AddUserValidator<TValidator>() where TValidator : class
     19         => AddScoped(typeof(IUserValidator<>).MakeGenericType(UserType), typeof(TValidator));
     20     public virtual IdentityBuilder AddClaimsPrincipalFactory<TFactory>() where TFactory : class
     21         => AddScoped(typeof(IUserClaimsPrincipalFactory<>).MakeGenericType(UserType), typeof(TFactory));
     22     public virtual IdentityBuilder AddErrorDescriber<TDescriber>() where TDescriber : IdentityErrorDescriber
     23     {
     24         Services.AddScoped<IdentityErrorDescriber, TDescriber>();
     25         return this;
     26     }
     27     public virtual IdentityBuilder AddPasswordValidator<TValidator>() where TValidator : class
     28         => AddScoped(typeof(IPasswordValidator<>).MakeGenericType(UserType), typeof(TValidator));
     29     public virtual IdentityBuilder AddUserStore<TStore>() where TStore : class
     30         => AddScoped(typeof(IUserStore<>).MakeGenericType(UserType), typeof(TStore));
     31     public virtual IdentityBuilder AddTokenProvider<TProvider>(string providerName) where TProvider : class
     32         => AddTokenProvider(providerName, typeof(TProvider));
     33     public virtual IdentityBuilder AddTokenProvider(string providerName, Type provider)
     34     {
     35         if (!typeof(IUserTwoFactorTokenProvider<>).MakeGenericType(UserType).GetTypeInfo().IsAssignableFrom(provider.GetTypeInfo()))
     36         {
     37             throw new InvalidOperationException(Resources.FormatInvalidManagerType(provider.Name, "IUserTwoFactorTokenProvider", UserType.Name));
     38         }
     39         Services.Configure<IdentityOptions>(options =>
     40         {
     41             options.Tokens.ProviderMap[providerName] = new TokenProviderDescriptor(provider);
     42         });
     43         Services.AddTransient(provider);
     44         return this;
     45     }
     46     public virtual IdentityBuilder AddUserManager<TUserManager>() where TUserManager : class
     47     {
     48         var userManagerType = typeof(UserManager<>).MakeGenericType(UserType);
     49         var customType = typeof(TUserManager);
     50         if (!userManagerType.GetTypeInfo().IsAssignableFrom(customType.GetTypeInfo()))
     51         {
     52             throw new InvalidOperationException(Resources.FormatInvalidManagerType(customType.Name, "UserManager", UserType.Name));
     53         }
     54         if (userManagerType != customType)
     55         {
     56             Services.AddScoped(customType, services => services.GetRequiredService(userManagerType));
     57         }
     58         return AddScoped(userManagerType, customType);
     59     }
     60     public virtual IdentityBuilder AddRoles<TRole>() where TRole : class
     61     {
     62         RoleType = typeof(TRole);
     63         AddRoleValidator<RoleValidator<TRole>>();
     64         Services.TryAddScoped<RoleManager<TRole>>();
     65         Services.AddScoped(typeof(IUserClaimsPrincipalFactory<>).MakeGenericType(UserType), typeof(UserClaimsPrincipalFactory<,>).MakeGenericType(UserType, RoleType));
     66         return this;
     67     }
     68     public virtual IdentityBuilder AddRoleValidator<TRole>() where TRole : class
     69     {
     70         if (RoleType == null)
     71         {
     72             throw new InvalidOperationException(Resources.NoRoleType);
     73         }
     74         return AddScoped(typeof(IRoleValidator<>).MakeGenericType(RoleType), typeof(TRole));
     75     }
     76     public virtual IdentityBuilder AddPersonalDataProtection<TProtector, TKeyRing>()
     77         where TProtector : class,ILookupProtector
     78         where TKeyRing : class, ILookupProtectorKeyRing
     79     {
     80         Services.AddSingleton<IPersonalDataProtector, DefaultPersonalDataProtector>();
     81         Services.AddSingleton<ILookupProtector, TProtector>();
     82         Services.AddSingleton<ILookupProtectorKeyRing, TKeyRing>();
     83         return this;
     84     }
     85     public virtual IdentityBuilder AddRoleStore<TStore>() where TStore : class
     86     {
     87         if (RoleType == null)
     88         {
     89             throw new InvalidOperationException(Resources.NoRoleType);
     90         }
     91         return AddScoped(typeof(IRoleStore<>).MakeGenericType(RoleType), typeof(TStore));
     92     }
     93     public virtual IdentityBuilder AddRoleManager<TRoleManager>() where TRoleManager : class
     94     {
     95         if (RoleType == null)
     96         {
     97             throw new InvalidOperationException(Resources.NoRoleType);
     98         }
     99         var managerType = typeof(RoleManager<>).MakeGenericType(RoleType);
    100         var customType = typeof(TRoleManager);
    101         if (!managerType.GetTypeInfo().IsAssignableFrom(customType.GetTypeInfo()))
    102         {
    103             throw new InvalidOperationException(Resources.FormatInvalidManagerType(customType.Name, "RoleManager", RoleType.Name));
    104         }
    105         if (managerType != customType)
    106         {
    107             Services.AddScoped(typeof(TRoleManager), services => services.GetRequiredService(managerType));
    108         }
    109         return AddScoped(managerType, typeof(TRoleManager));
    110     }
    111 }
    IdentityBuilder

    八、基于RazorPages的UI

    IdentityServiceCollectionExtensions

    AddIdentity  ConfigureApplicationCookie ConfigureExternalCookie

  • 相关阅读:
    Mysql主键索引、唯一索引、普通索引、全文索引、组合索引的区别
    centos7下安装phpmyadmin
    centos7下apache启动报错记录
    Centos7 升级php版本到php7
    centos7下安装composer和git
    git推送新项目到github
    git使用入门
    laravel5.5学习2-路由系统
    laravel5.5入门-安装和认证
    laravel的monolog使用
  • 原文地址:https://www.cnblogs.com/dongshenjun/p/12717385.html
Copyright © 2020-2023  润新知