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 的请求。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM