【发布时间】:2021-12-31 07:18:44
【问题描述】:
我有一个使用 SwaggerGen 记录的 .NET 5 API 项目,我正在尝试使用 Swashbuckle 作为文档 UI。我的身份验证提供程序是 Auth0,因此我希望通过对 Auth0 /oauth/token 端点进行有效的 OAuth2 调用来让文档生成 JWT 不记名令牌。 Authorize 按钮出现在生成的页面上,并生成一个表单,要求用户输入 client_id 和 client_secret,但是当我按下 Authorize 按钮时,它会发出一个缺少 POST 请求的 @987654327 @ 和 client_secret。具体来说,它会转到正确的端点 (/oauth/token),但没有查询字符串参数,POST 正文中只有 grant_type: client_credentials。我可以在 Chrome 开发者工具中看到这一点。不知何故,UI 完全忽略了我在 client_id 和 client_secret 表单字段中输入的值。
让身份验证请求使用表单中的值是否有技巧?这是我的 SwaggerGen 配置的相关部分:
options.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme {
Type = SecuritySchemeType.OAuth2,
Name = "Bearer",
Description = "Authorization using the OAuth2 access token authorization flow",
Scheme = "Bearer",
In = ParameterLocation.Header,
Flows = new OpenApiOAuthFlows {
ClientCredentials = new OpenApiOAuthFlow {
TokenUrl = new Uri($"https://{_configuration["Auth0:HostedDomain"]}/oauth/token"),
AuthorizationUrl = new Uri($"https://{_configuration["Auth0:HostedDomain"]}/authorize")
}
}
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme {
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme,
Id = "OAuth2"
}
},
new List<string>()
}
});
【问题讨论】:
-
您找到解决方案了吗?我也有同样的问题...
-
不。我最终放弃了 Swagger,转而使用 Stoplight。 Stoplight 没有提供在测试工具中进行身份验证的简单方法,但它看起来比 Swagger 专业得多,并且至少不会向用户显示损坏的身份验证表单。
标签: swagger auth0 swashbuckle