IdentityServer4 關於 AddIdentityServerAuthentication 方法


AddIdentityServerAuthentication 是 IdentityServer 官方封裝的用於認證的方法,接入 ASP.NET Core 的認證機制,包含在組件 IdentityServer4.AccessTokenValidation 中。

在 ASP.NET Core 早期,1.1、2.0(2.0不確定,時間太久了)時 AddIdentityServerAuthentication 還是 IdentityServer 官方文檔及示例代碼提供的注冊認證的方法,后面都變更為了:

services.AddAuthentication("Bearer")
                .AddJwtBearer()

一度我曾經以為 AddIdentityServerAuthentication 無用了,現在我更正我的想法,這個方法同時支持了 Reference Token 和 JWT 的認證,所以說如果使用 Reference Token 還是要使用這個方法的。
當然,如果使用 JWT 的話還是推薦直接使用 AddJwtBearer ,這是微軟官方提供的支持JWT的認證組件,不用額外安裝 Nuget 包。

2020.10.15 再次更新

IdentityServer4.AccessTokenValidation 組件的 github 倉庫已經存檔了,然后我查詢了官方文檔,已經不使用這個組件了,對於 Reference Token 的驗證,使用 https://github.com/IdentityModel/IdentityModel.AspNetCore.OAuth2Introspection

如果同時支持 JWT 和 Reference Token ,那么這樣寫:

services.AddAuthentication("token")

    // JWT tokens
    .AddJwtBearer("token", options =>
    {
        options.Authority = Constants.Authority;
        options.Audience = "resource1";

        options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };

        // if token does not contain a dot, it is a reference token
        options.ForwardDefaultSelector = Selector.ForwardReferenceToken("introspection");
    })

    // reference tokens
    .AddOAuth2Introspection("introspection", options =>
    {
        options.Authority = Constants.Authority;

        options.ClientId = "resource1";
        options.ClientSecret = "secret";
    });

相關資料:

https://docs.identityserver.io/en/latest/topics/apis.html

https://docs.identityserver.io/en/latest/topics/reference_tokens.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM