在 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 的請求。