【问题标题】:Swashbuckle Swagger UI not sending client_secret and client_id to OAuth endpoint when using authentication form使用身份验证表单时,Swashbuckle Swagger UI 未将 client_secret 和 client_id 发送到 OAuth 端点
【发布时间】:2021-12-31 07:18:44
【问题描述】:

我有一个使用 SwaggerGen 记录的 .NET 5 API 项目,我正在尝试使用 Swashbuckle 作为文档 UI。我的身份验证提供程序是 Auth0,因此我希望通过对 Auth0 /oauth/token 端点进行有效的 OAuth2 调用来让文档生成 JWT 不记名令牌。 Authorize 按钮出现在生成的页面上,并生成一个表单,要求用户输入 client_idclient_secret,但是当我按下 Authorize 按钮时,它会发出一个缺少 POST 请求的 @987654327 @ 和 client_secret。具体来说,它会转到正确的端点 (/oauth/token),但没有查询字符串参数,POST 正文中只有 grant_type: client_credentials。我可以在 Chrome 开发者工具中看到这一点。不知何故,UI 完全忽略了我在 client_idclient_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


【解决方案1】:

您确定 Swagger UI 没有发送它们,即在授权标头中?

我遇到了类似的问题,因为我们的 OpenID 服务器仅识别以正文形式发送的客户端凭据(client_idclient_secret),我们必须在授权对话框中选择正确的"Client credentials location" 选项(Request body) :


然后client_id在请求正文中正确发送:

【讨论】:

  • 这适用于password 流,但使用application 流时没有“客户端凭据位置”对话框...
猜你喜欢
  • 2022-10-04
  • 2017-05-06
  • 2021-09-23
  • 1970-01-01
  • 2022-01-24
  • 2022-12-21
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
相关资源
最近更新 更多