Swagger(C#)不添加授权标题
我试图让Swagger与客户凭证流程一起工作。
builder.Services.AddSwaggerGen(c =>
{
c.SchemaFilter<SchemaFilter>();
c.SwaggerDoc("v1", new OpenApiInfo { Title = "The API", Version = "v1" });
var basePath = System.AppContext.BaseDirectory;
string docFileName = Directory.GetFiles(basePath, "*.xml").First();
var docFile = Path.Combine(basePath, docFileName);
if (File.Exists(docFile))
{
c.IncludeXmlComments(docFile);
}
// Makes the UI appear and be able to get a token.
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
{
Description = "Bearer token",
In = ParameterLocation.Header,
Name = "Authorization",
Scheme = "Bearer",
BearerFormat = "JWT",
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
ClientCredentials = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri("http://www.localhost:5000/connect/authorize"),
TokenUrl = new Uri("http://www.localhost:5000/connect/token"),
Scopes = new Dictionary<string, string>() { { "thescope", "The scope" } }
}
}
});
// Docs claim this is necessary; doesn't say what it does in addition to the above.
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme
},
Scheme = "Bearer", // https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
In = ParameterLocation.Header
},
new List<string>()
}
});
在Swagger网页上出现 授权按钮,我可以使用client_id
和client_secret
获得令牌。
但是,Swagger不使用令牌。没有授权
将标题添加到任何请求中。为什么?
I'm trying to get Swagger working with a client credentials flow.
builder.Services.AddSwaggerGen(c =>
{
c.SchemaFilter<SchemaFilter>();
c.SwaggerDoc("v1", new OpenApiInfo { Title = "The API", Version = "v1" });
var basePath = System.AppContext.BaseDirectory;
string docFileName = Directory.GetFiles(basePath, "*.xml").First();
var docFile = Path.Combine(basePath, docFileName);
if (File.Exists(docFile))
{
c.IncludeXmlComments(docFile);
}
// Makes the UI appear and be able to get a token.
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
{
Description = "Bearer token",
In = ParameterLocation.Header,
Name = "Authorization",
Scheme = "Bearer",
BearerFormat = "JWT",
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows()
{
ClientCredentials = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri("http://www.localhost:5000/connect/authorize"),
TokenUrl = new Uri("http://www.localhost:5000/connect/token"),
Scopes = new Dictionary<string, string>() { { "thescope", "The scope" } }
}
}
});
// Docs claim this is necessary; doesn't say what it does in addition to the above.
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme
},
Scheme = "Bearer", // https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
In = ParameterLocation.Header
},
new List<string>()
}
});
On the Swagger web page an Authorize button appears and I can use the client_id
and client_secret
to get a token.
However, Swagger does not use the token. No Authorize
header is added to any requests. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论