asp.net core 中的认证方式(Authentication Scheme)

https://docs.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme?view=aspnetcore-3.1

在 SPA 中有时候会同时使用多种认证方式。比如,使用基于 cookie 的认证方式来登录,并且对 javascript 请求使用 jwt bearer 认证。也可能有多个认证 handler. 比如用2个 cookie handlers. 1个包含基本的 identity, 而另一个用于启用了多因素认证的情况。

注册方式为在 Startup 的 ConfigureServices() 中,使用 services.AddAuthentication() 方法。

用 Authorize attribute 标注可以选择使用哪种 scheme.

也可以将 authentication scheme 注册到 policy 里面,然后在 Authorize attribute 中注明选择哪个 policy.

可以 override 默认的 authorization policy, 通过 services.AddAuthorization() 方法。
如果 override 了,那么在 controller 上就可以简单的使用 [Authorize] 来标注接受来自带有任一个 authentication scheme 颁发的 token 的请求。

原文地址:https://www.cnblogs.com/new-start/p/13237500.html