gpt4 book ai didi

javascript - 带有客户端凭证流的 Swashbuckle OAuth2 授权

转载 作者:数据小太阳 更新时间:2023-10-29 05:35:06 24 4
gpt4 key购买 nike

我使用 Swashbuckle 来记录 WebAPI Controller 。我还使用 OAuth2 和客户端凭证流。所以要授权,我需要传递 client_idclient_secret

我有以下代码:

config.EnableSwagger(c => {
c.SingleApiVersion("v1", "My API");
c.OAuth2("oauth2")
.Flow("application")
.TokenUrl("/oauth2/token");
c.OperationFilter<AssignOAuthSecurityRequirements>();
})
.EnableSwaggerUi(c => {
c.EnableOAuth2Support(clientId: "clientIdValue", clientSecret:"clientSecretValue", "", "");
c.CustomAsset("index", Assembly.GetExecutingAssembly(), "WebAPI.Swagger.UI.index.html");
});

授权工作正常,但我的 client_idclient_secret 值是硬编码的(clientIdValue、clientSecretValue)。我怎样才能增加用户在此对话框中输入该值的可能性?谁能帮帮我?

enter image description here

如果我也需要发布 AssignOAuthSecurityRequirements 的代码,请告诉我。提前致谢

最佳答案

不确定您的代码到底出了什么问题,可能是缺少范围定义。

我已经使用 ASP.NET Core 和当前版本的 Swashbuckle.AspNetCore ( https://github.com/domaindrivendev/Swashbuckle.AspNetCore ) 成功完成了它

客户端凭据流称为“应用程序”,因此,在您的 Startup.cs 文件中,您需要按如下方式配置 Swagger:

        services.AddSwaggerGen(c => {

//other configs...

c.AddSecurityDefinition("oauth2", new OAuth2Scheme {
Type = "oauth2",
Flow = "application",
TokenUrl = "<token_endpoint_url>",
Scopes = new Dictionary<string, string>
{
{ "first-scope", "First scope description" },
{ "second-scope", "Second scope description" }
//define as many scopes as you want...
}
});
});

TokenUrl 参数必须指向一个有效的 OAuth 2.0 兼容 token 端点(查看 http://docs.identityserver.io/en/release/endpoints/token.html 以获取有关端点应如何表现/外观的示例)。绝对和相对 URL 在我的测试中都有效。

之后,授权对话框应该如下所示:

Authorize popup

  • 请注意,在授权按钮实际提交任何内容之前,您需要至少选择一个范围(应该更改 oauth 组件以添加免责声明恕我直言)。

SwaggerUI 部分不需要额外的配置。

关于javascript - 带有客户端凭证流的 Swashbuckle OAuth2 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42558039/

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