gpt4 book ai didi

c# - 使用 JwtBearerAuthentication 签名 key

转载 作者:太空狗 更新时间:2023-10-29 20:09:36 24 4
gpt4 key购买 nike

我正在尝试使用 JwtBearerMiddleware 在我的 AspNetCore MVC 应用程序(仅限 Web API)中实现 JWT Bearer 身份验证,但我收到了带有 header 的 401 响应:

WWW-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"

Startup.cs 中的相关代码如下所示:

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
Authority = "https://example.okta.com",
Audience = "myClientId"
});

有了权限 URL,我希望中间件从 https://example.okta.com/.well-known/openid-configuration 查询我的身份提供者元数据以获取 jwks_uri 然后从 https://example.okta.com/oauth2/v1/keys 获取签名 key 。我认为这不会发生。我需要做什么才能找到并使用签名 key ?谢谢

最佳答案

在遵循引用资料并深入研究 AspNet Security repo 之后(特别是 JwtBearerHandlerJwtBearerMiddleware 类),这让我找到了位于 Azure Extensions repo 中的 Microsoft.IdentityModel 命名空间(首先是 ConfigurationManager<T> 类,然后是 OpenIdConnectConfigurationRetriever 类(GetAsync 方法),然后是 JsonWebKeySet.GetSigningKeys() 方法),我终于发现 JwtBearerMiddleware 确实从元数据中的 jwks_uri。呸。

那么为什么它不起作用?我应该早点检查的是 Bearer JWT header 中的 kid 实际上与 kid 中的任何一个都不匹配来自 jwks_uri,因此未找到。这是我作为不记名 token 发送的 access_code。另一方面,id_token 确实有一个匹配的 kid,所以使用它就可以了!

我读过:

The OIDC Access Token is applicable only for the Okta /oauth2/v1/userinfo endpoint and thus should be treated as opaque by the application. The application does not need to validate it since it should not be used against other resource servers. The format of it and the key used to sign it are subject to change without prior notice. source

...所以我不能使用访问 token 。

关于c# - 使用 JwtBearerAuthentication 签名 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38394545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com