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