Loading

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

posted @ 2020-06-11 19:03  晓晨Master  阅读(7318)  评论(9编辑  收藏  举报