zoukankan      html  css  js  c++  java
  • asp.net core 3.x 身份验证-2启动阶段的配置

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

    在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 defaultScheme)内部也是调用上面的方法,只是只设置了AuthenticationOptions.DefaultScheme

    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();

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

    身份验证方案运行时容器AuthenticationSchemeProvider的建立

    由于AuthenticationSchemeProvider是以单例形式注册到依赖注入容器中的,因此应该在注册时(不是很确定,假设吧)就会创建AuthenticationSchemeProvider,它通的构造函数定义了IOptions<AuthenticationOptions>参数,上面说了AuthenticationOptions就包含上面注册进去的身份验证方案列表,AuthenticationSchemeProvider在构造函数中遍历,将所有注册的身份验证方案存储到自身的 IDictionary<string, AuthenticationScheme> _schemes 变量中

    插入身份验证中间件

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

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

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

  • 相关阅读:
    14.4 exportfs命令 14.5 NFS客户端问题 15.1 FTP介绍 15.2/15.3 使用vsftpd搭建ftp
    14.1 NFS介绍 14.2 NFS服务端安装配置 14.3 NFS配置选项
    13.4 mysql用户管理 13.5 常用sql语句 13.6 mysql数据库备份恢复
    13.1 设置更改root密码 13.2 连接mysql 13.3 mysql常用命令
    12.21 php-fpm的pool 12.22 php-fpm慢执行日志 12.23 open_basedir 12.24 php-fpm进程管理
    12.17 Nginx负载均衡 12.18 ssl原理 12.19 生成ssl密钥对 12.20 Nginx配置ssl
    12.13 Nginx防盗链 12.14 Nginx访问控制 12.15 Nginx解析php相关配置 12.16 Nginx代理
    在界面workspacebar上添加的OnContextMenu函数打的断点始终进不去,显示当前不会命中断点还未为文档加载任何符号
    VS2019安装闪退, 不出现安装界面,解决办法
    BCGP单元格多列合并需要注意的
  • 原文地址:https://www.cnblogs.com/jionsoft/p/12301179.html
Copyright © 2011-2022 走看看