asp.net core 3.x 身份验证-2启动阶段的配置

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

asp.net core 3.x 身份验证-2启动阶段的配置

变形精怪   2020-02-13 我要评论

注册服务、配置选项、添加身份验证方案

在Startup.ConfigureServices执行services.AddAuthentication()

注册如下服务(便于理解省略了部分辅助服务):

  • services.TryAddScoped<IAuthenticationService, AuthenticationService>();
  • services.TryAddScoped<IAuthenticationHandlerProvider, AuthenticationHandlerProvider>();
  • services.TryAddSingleton<IAuthenticationSchemeProvider, AuthenticationSchemeProvider>();

整个应用的身份验证有个选项对象叫AuthenticationOptions(上一篇有描述),允许我们对身份验证做整体配置,这个配置主要体现为:配置系统需要支持的身份验证方案列表;指定默认身份验证方案、默认登录时用的身份验证方案...默认注销...等。这个对象的应用使用asp.net core的选项模型,我们可以通过AddAuthentication(Action<AuthenticationOptions>)重载来进行配置。参考如下代码:

1 services.AddAuthentication(authenticationOptions=> {
2    authenticationOptions.AddScheme<CookieAuthenticationHandler>("cookie", "显示名cookie");
3    authenticationOptions.AddScheme<JwtBearerHandler>("jwt","显示名jwtToken");
4    authenticationOptions.DefaultAuthenticateScheme = "cookie";
5    //...其它配置
6 });

此重载同样会先注册上面的核心服务。然后设置初始化AuthenticationOptions的委托,当某个类注入AuthenticationOptions时,依赖注入框架会调用此委托来初始化这个选项对象。

另一个重载AddAuthentication(string defaultAuthenticateScheme)内部也是调用上面的方法,只是只设置了AuthenticationOptions.DefaultAuthenticateScheme

AddAuthentication方法始终返回AuthenticationBuilder,它允许我们通过链式调用方式来向AuthenticationOptions添加多个身份验证方案,所以更常见的方式如下:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie().AddJwtBearer();

CookieAuthenticationDefaults.AuthenticationScheme常来指明将哪个身份验证方案作为默认。后续分别添加了cookie和JwtBearer两个身份验证方案。

我们说AuthenticationOptions是针对整个应用程序的身份验证选项,可以简单理解为它是身份验证方案的配置时容器。针对特定身份验证方案也有自己的配置对象。以AddCookie()为例,它等同于:

 1 authenticationOptions.AddScheme<CookieAuthenticationHandler>(CookieAuthenticationDefaults.AuthenticationScheme, "显示名cookie",);
 2 service.Configre<CookieAuthenticationOptions>(options=>{
 4            options.Cookie.Name = CookieAuthenticationDefaults.CookiePrefix + name;
 6            options.CookieManager = new ChunkingCookieManager();
 7            options.LoginPath = CookieAuthenticationDefaults.LoginPath;
 8            options.LogoutPath = CookieAuthenticationDefaults.LogoutPath;
 9            options.AccessDeniedPath = CookieAuthenticationDefaults.AccessDeniedPath;
10       }
11 });

CookieAuthenticationOptions就是针对这个cookie身份验证方案的选项对象,将来某个类注入此选项对象时,依赖注入框架会回调此委托来初始化它。参考:选项模型。当然AddCookie有对应的重载允许我们自己的委托来初始化这个选项对象。类似下面的代码:

1 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie("换个方案名","显示名",opt=> {
2       opt.SlidingExpiration = true;//滑动过期?
3       //设置其它跟cookie身份验证相关的东东...
4 }).AddJwtBearer();

现在看看这段代码就亲切多了。

 

插入身份验证中间件

上面只是注册了身份验证过程中需要的服务并配置了应用需要支持身份验证方案列表,在将来请求抵达时需要一个中间件来处理身份验证,核心任务是找到默认身份验证处理器,通过它从请求中获得当前用户标识,然后设置到httpContext.User属性上,至于处理的具体过程将在下一篇基于cookie的身份验证整体流程详细说。在Startup.Configre中通过扩展方法注册:

app.UseRouting();
app.UseAuthentication();
app.UseEndpoints(endpoints =>{
  endpoints.MapRazorPages();
});

至于为啥身份验证中间件一定要在 app.UseRouting(); 之后,我是真的想不通....

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们