Swagger 过渡到Swashback 5.0

Swagger 过渡到Swashback 5.0,swagger,swashbuckle,Swagger,Swashbuckle,我通过Swashback V4使用Swagger,通过使用API密钥对端点进行身份验证 使用Swashback V4时,以下配置工作正常(请注意,仅显示Swagger代码): public void配置服务(IServiceCollection服务){ services.AddSwaggerGen(c=>{ c、 招摇过市的医生( “v1”, 新信息{ Title=“OAPI”,Version=“v1” }); c、 AddSecurityDefinition(“api_密钥”),新的ApiKe

我通过Swashback V4使用Swagger,通过使用API密钥对端点进行身份验证

使用Swashback V4时,以下配置工作正常(请注意,仅显示Swagger代码):

public void配置服务(IServiceCollection服务){
services.AddSwaggerGen(c=>{
c、 招摇过市的医生(
“v1”,
新信息{
Title=“OAPI”,Version=“v1”
});
c、 AddSecurityDefinition(“api_密钥”),新的ApiKeyScheme{
In=“query”,
Description=“请输入身份验证令牌”,
Name=“key”,
Type=“apiKey”
});
c、 AddSecurityRequest(新字典>{
{
“api_密钥”,
新[]{
“读访问”,
“writeAccess”
}
}
});
});
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境){
app.UseSwagger();
app.UseSwaggerUI(c=>{
c、 SwaggerEndpoint(“/api/swagger/v1/swagger.json”,“api v1”);
c、 RoutePrefix=“api/swagger”;
});
}
Swashback包含一个“转换到Swashback 5.0”主题,但它不包括API键的使用

我无法找到一个关于如何转换到V5的完整示例,因此我分析了方法签名以生成相同的配置

以下代码假装复制上述V4配置:

public void配置服务(IServiceCollection服务){
var securityScheme=新的OpenApiSecurityScheme{
In=ParameterLocation.Query,
Description=“请输入身份验证令牌”,
Name=“key”,
类型=SecuritySchemeType.ApiKey
};
services.AddSwaggerGen(c=>{
c、 SwaggerDoc(“V1”),新OpenApiInfo{
Version=“V1”,
Title=“API”,
Description=“API”
});
c、 AddSecurityDefinition(“api_密钥”,securityScheme);
c、 AddSecurityRequest(新的OpenAPISecurityRequest{
{
担保计划,
新[]{
“读访问”,
“writeAccess”
}
}
});
c、 EnableAnnotations();
});
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境){
app.UseSwagger();
app.UseSwaggerUI(c=>{
c、 SwaggerEndpoint(“/api/swagger/v1/swagger.json”,“api v1”);
c、 RoutePrefix=“api/swagger”;
});
}
在运行API时,会显示“Swagger身份验证”窗口,我可以使用API密钥进行身份验证

不幸的是,在执行任何端点时,我收到以下错误:

 System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.

        at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync
        at Microsoft.AspNetCore.Mvc.ChallengeResult.ExecuteResultAsync
        at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync
        at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]
        at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow
        at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync]
        at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAlwaysRunResultFilters
        at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync
        at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync
        at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke
        at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke
        at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke
        at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke
        at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke
        at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore

我想我遗漏了什么,我试着研究课程,但到目前为止我没有运气。这可能是一个小细节,但到目前为止还无法理解…

您需要在使用
AddSecurityRequirement
时参考该方案,因为您正在全局应用它。 此外,当使用除“oauth2”以外的任何其他类型时,作用域数组必须为空

下面的例子应该有用

c.AddSecurityRequirement(新的OpenApiSecurityRequirement
{
{
新的OpenApiSecurityScheme
{
Reference=新的OpenApiReference
{
Id=“api_密钥”,
Type=ReferenceType.SecurityScheme
}
},
新列表()}
});

除了执行@Pavlos所说的操作外,还需要使用AddAuthentication(字符串defaultScheme)指定默认的authenticationScheme。 例如:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
或在RequireAuthorization方法中指定授权数据

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers()
        .RequireAuthorization(new AuthorizeAttribute() { AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme });
});
您可以使用多个AuthenticationScheme,通过逗号连接它们