Asp.NET Core 身份验证(Authentication)概览
原文地址: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-5.0
身份验证是判断用户身份的过程。授权(Authorization)是判断用户是否对一个资源有权限的过程。
身份验证中间件使用 IAuthenticationService 完成身份验证过程。
这个服务调用注册的身份验证 handlers 完成身份验证相关的动作。
包括:
- 对用户进行身份验证
 - 当未经身份验证的用户试图访问受限制的资源时作出响应
 
注册的身份验证 handlers 以及它们的配置项被称作 schemes。
Authentication Schemes 在 Startup.ConfiguraService 注册 Authentication 服务时指定:
- 在
services.AddAuthentication之后调用 (比如AddJwtBearer或者AddCookie) - 或者直接调用 
AuthenticationBuilder.AddScheme方法 (AddJwtBearer,AddCookie也是通过这种方式实现的) 
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => Configuration.Bind("JwtSettings", options))
    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => Configuration.Bind("CookieSettings", options));
AddAuthentication 的参数 JwtBearerDefaults.AuthenticationScheme 是在未指定sheme的请求是默认使用的 sheme 。
如果使用了多个 scheme, 授权策略(或者说authorization attributes) 可以指定他们验证用户身份所依赖的 一个或者多个 scheme。 上面的例子中,如果想使用 cookie 身份认证 的scheme 可以通过指定它的名字。(默认名字是CookieAuthenticationDefaults.AuthenticationScheme, 也可以在调用 AddCookie 使用其他的名字 )
某些情况下, AddAuthentication 会被别的扩展方法自动调用。例如,在使用 ASP.NET Core Identity时, AddAuthentication 就会被被内部调用。
Startup.Configure 中通过调用 IApplicationBuilder 的扩展方法 UseAuthentication 注册身份认证中间件。该中间件会使用之前注入的 身份认证 schemes。在需要使用到已经经过身份认证的用户信息的中间件之前调用 UseAuthentication 方法。如果使用 endpoint routing,
- 调用 UseAuthencation 方法之前调用 UseRouting, 这样路由信息就可以在身份认证决策时已经准备好了;
 - 调用 UseAuthentication 方法之后调用 UseEndpoints, 这样可以确保用户接触到endpoint时已经是认证过的。
 
Authentication Concepts
可通过多种多种方法来选择使用哪种 authentication hanlder 来生成正确的 Claim 集。
- Authentication Sheme
 - 设置默认 Authentication Sheme
 - 直接给 HttpContext.User 赋值.
 
如果没有指定默认的 sheme, 并且在 authorize attribute 中也没有指定要使用的 scheme, 就会产生以下错误:
InvalidOperationException: No authenticationScheme was specified, and there was no DefaultAuthenticateScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).
Authentication scheme
主要是用来通过 scheme 名字指定以及配置相对应的 authentication handler。
通常在配置 authentication 时会指定默认的 scheme。除非请求资源时使用了特定的scheme,其余情况下都会去使用默认的scheme。 也可以:
- 对 authenticate, challenge, forbid 行为 指定不同的默认sheme
 - 使用 policy schemes 合并多个 schemes到一个里面。
 
Authentication handler
- 实现了相应sheme的需求
 - 派生自  
[IAuthenticationHandler](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.iauthenticationhandler?view=aspnetcore-5.0)or[AuthenticationHandler<TOptions>](https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.authenticationhandler-1?view=aspnetcore-5.0). - 主要责任是对用户进行身份验证
 
基于 authentication scheme 的配置以及 请求的上下文, authentication handler 会
- 生成 
AuthenticationTicket对象, 用来表示用户的身份验证成功 - 返回 空结果 或者 错误结果 表示 验证失败
 - 当用户尝试获取某些资源时提供方法去 判别(challenge,没有认证) 或者 禁止(forbid,没有权限)
 
Authenticate 认证操作
Authenticate 操作是基于请求上下文构建用户的身份。 返回 AuthenticateResult 表示用户身份认证是否成功成功,如果成功的话,则还在 authentication ticket 指示用户的身份。 参考AuthenticateAsync。身份验证示例包括:
- 根据 cookie 构造用户身份的 cookie 身份验证方案。
 - 对 JWT 持有者令牌进行反序列化和验证以构造用户身份的 JWT 持有者方案。
 
Challenge
当未经身份验证的用户请求要求身份验证的终结点时,授权会发起身份验证挑战。 例如,当匿名用户请求受限资源或单击登录链接时,会引发身份验证挑战。 授权会使用指定的身份验证方案发起挑战;
                    
                
                
            
        
浙公网安备 33010602011771号