C# 如何在Swagger 5和Asp Core 3中混合Oauth2与JwtBearer?
下面是我向asp core的服务集合添加swagger的代码:C# 如何在Swagger 5和Asp Core 3中混合Oauth2与JwtBearer?,c#,asp.net-core,oauth-2.0,jwt,swagger,C#,Asp.net Core,Oauth 2.0,Jwt,Swagger,下面是我向asp core的服务集合添加swagger的代码: services.AddSwaggerGen(options => { var xmlDocPath = Path.Combine(AppContext.BaseDirectory, "Api.xml"); options.IncludeXmlComments(xmlDocPath, true); op
services.AddSwaggerGen(options =>
{
var xmlDocPath = Path.Combine(AppContext.BaseDirectory, "Api.xml");
options.IncludeXmlComments(xmlDocPath, true);
options.SwaggerDoc("v1", new OpenApiInfo {Version = "v1", Title = "Api V1 Docs"});
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
BearerFormat = "Bearer {Token}",
Name = "oauth2",
Scheme = "bearer",
In = ParameterLocation.Header,
Flows = new OpenApiOAuthFlows
{
Password = new OpenApiOAuthFlow
{
TokenUrl = new Uri("https://localhost:44362/api/Users/GenerateToken", UriKind.Absolute),
}
}
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "oauth2"},
},
new string[] { }
}
});
});
当我单击swagger中的授权按钮并填写凭据(用户名和密码)时,它将获得授权,但当我向服务器承载令牌发送请求时,标题中的“未定义”:
有什么问题?
我应该怎么做?从
中删除BearPerformat
、名称
、方案
和中的。AddSecurityDefinition(…)
(这些参数不用于OAuth 2安全方案)。这有帮助吗?@Helen我照你说的做了,没有修复,但也没有改变任何东西,所以它们是额外的。你是如何修复这个错误的?从中删除BearPerformat
、Name
、Scheme
和中的。AddSecurityDefinition(…)
(这些参数不用于OAuth 2安全方案)。这有帮助吗?@Helen我照你说的做了,没有修复,但也没有改变任何东西,所以它们是额外的。你是如何修复这个错误的?