gpt4 book ai didi

asp.net-core - 使用 .Net Core 中的 PKCE 和 Okta 从 Swagger UI 连接 OpenID

转载 作者:行者123 更新时间:2023-12-04 12:34:02 26 4
gpt4 key购买 nike

过去 4 周在调试器中绕过 Controller 授权后,我最终决定在我支持 Swashbuckle 的 .NetCore 5 API 中处理 OpenID Connect 身份验证。我希望我没有,因为到目前为止我花了将近一天的时间没有找到可行的解决方案。

这里是一个简短的回顾。

  • 最近才支持 Swagger-UI 中的 OpenID Connect。我发现此信息的唯一地方是 Helen 对 this question 的评论。 . Swagger Ui 3.38.0 仅在 Swashbuckle 6.0.7 中可用。
  • 升级到最新的 Swashbuckle 后,我开始在 Swagger UI 中看到一堆“已发现”的授权选项。唉,基于错误,PKCE 似乎没有被使用,即使我在 Startup.cs 中明确设置了它:

.UseSwaggerUI(c => c.OAuthUsePkce());

另外,那里的 ClientSecret 没有意义,因为 PKCE 应该替换它(实际上我没有客户端密码)。我的问题,是否有人在 Swagger UI 中使用 OpenID Connect with PKCE 和 Okta?

enter image description here

Auth ErrorError,错误:invalid_client,描述:浏览器对 token 端点的请求必须使用 Proof Key 进行代码交换。

Okta application settings

最佳答案

我最近从隐式流转为代码+pkce 流。我遇到了同样的问题。关键是配置 token 端点 url。 Swagger UI 仍会向您显示客户端凭据输入框,但您可以在授权时将其留空。

var securityDefinition = new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Scheme = "Bearer",
In = ParameterLocation.Header,
Name = "Authorization",
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri(azureAdOptions.AuthorizeEndpoint),
TokenUrl = new Uri(azureAdOptions.TokenEndpoint),
Scopes = azureAdOptions.Applications["Api"].Scopes.ToDictionary(e => e.Value, e => e.Key)
}
}
};

c.AddSecurityDefinition(
"oauth2",
securityDefinition);

我显然仍然需要在 SwaggerUiOptions 上启用 pkce 支持

internal static void ConfigureUi(SwaggerUIOptions c, IConfiguration configuration, string apiName, string environmentName)
{
c.OAuthUsePkce();
}

我使用 Azure AD,以下是我使用的值:

AuthorizationUrl: https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize
TokenUrl: https://login.microsoftonline.com/organizations/oauth2/v2.0/token
Scopes: custom-value

下面的提交包含了它是如何实现的所有细节。它还包含一个测试样本。 Add support to PKCE for SwaggerUI & update OAuth2Integration sample

关于asp.net-core - 使用 .Net Core 中的 PKCE 和 Okta 从 Swagger UI 连接 OpenID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66445473/

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