【问题标题】:How to validate IdentityServer4 access tokens through introspection如何通过自省验证 IdentityServer4 访问令牌
【发布时间】:2021-10-04 02:10:36
【问题描述】:

我正在通过 IdentityServer4 发布的 AccessToken 实现受保护的 api。现在我的问题是,当我从客户端使用标头中的 Bearer 令牌调用 API 时,是否有可能通过调用内省 endopint 来恢复该值并对其进行验证?没有像这样的密钥进行自我验证:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            // base-address of your identityserver
            options.Authority = "https://demo.identityserver.io";

            // if you are using API resources, you can specify the name here
            options.Audience = "resource1";

            // IdentityServer emits a typ header by default, recommended extra check
            options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" };
        });

是否有任何库可以通过依赖注入在授权中间件中完成所有操作,还是我必须手动完成所有操作?我试图环顾四周,但似乎唯一的库引用了引用令牌,而不是作为 Bearer 传递的访问令牌。 谢谢

【问题讨论】:

  • Oauth2Introspection 也许?:GitHubDocs
  • 万分感谢。我知道 IdentityModel 包,但不知道这个扩展。再次感谢

标签: c# asp.net-core oauth-2.0 identityserver4


【解决方案1】:

您可以挂钩OnMessageReceived 事件并自己验证令牌:

services.AddAuthentication(...)
    .AddJwtBearer(
        options => {
            options.Events.OnMessageReceived = async context => {
                var accessToken = context.Token;

                // inject a custom token validator
                var tokenValidator =
                    context.HttpContext.RequestServices.GetRequiredService<ITokenValidator>();
                var ok = await tokenValidator.ValidateTokenAsync(accessToken);
                if (!ok) {
                    context.Fail("Invalid token");
                }
            };
        }
    );

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 2019-02-07
    • 2018-01-21
    • 2018-05-24
    • 2017-05-14
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 2019-04-19
    相关资源
    最近更新 更多