Rob*_*ean 6 javascript c# swagger swagger-ui swashbuckle
我使用Swashbuckle来记录WebAPI控制器.我还使用OAuth2和Client Credentials Flow.所以授权我需要传递 client_id和client_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");
});
Run Code Online (Sandbox Code Playgroud)
授权工作正常但我client_id和client_secret值是硬编码的(clientIdValue,clientSecretValue).如何在此对话框中添加用户输入值的可能性?谁能帮我?
如果我还需要发布代码,请告诉我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...
}
});
});
Run Code Online (Sandbox Code Playgroud)
TokenUrl 参数必须指向有效的 OAuth 2.0 兼容令牌端点(查看http://docs.identityserver.io/en/release/endpoints/token.html以获取有关端点应如何表现/外观的示例)。绝对 URL 和相对 URL 在我的测试中都有效。
之后,授权对话框应如下所示:
SwaggerUI 部分不需要任何额外配置。