我通过Swashbuckle V4使用Swagger,通过使用API密钥对端点进行了验证。
在使用Swashbuckle V4时,以下配置工作得很好(请注意,只显示了Swagger代码):
public void ConfigureServices(IServiceCollection services) {
services.AddSwaggerGen(c => {
c.SwaggerDoc(
"v1",
new Info {
Title = "OAPI", Version = "v1"
});
c.AddSecurityDefinition("api_key", new ApiKeyScheme {
In = "query",
Description = "Please Enter Authentication Token",
Name = "key",
Type = "apiKey"
});
c.AddSecurityRequirement(new Dictionary < string, IEnumerable < string >> {
{
"api_key",
new [] {
"readAccess",
"writeAccess"
}
}
});
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/api/swagger/v1/swagger.json", "API V1");
c.RoutePrefix = "api/swagger";
});
}Swashbuckle GitHub页面包含一个“转换到SwashBackle5.0”的主题,但是它并没有涵盖一个API键的使用。
我无法找到一个关于如何转换到V5的完整示例,所以我分析了方法签名,以便生成相同的配置。
以下代码假装复制了上述V4配置:
public void ConfigureServices(IServiceCollection services) {
var securityScheme = new OpenApiSecurityScheme {
In = ParameterLocation.Query,
Description = "Please Enter Authentication Token",
Name = "key",
Type = SecuritySchemeType.ApiKey
};
services.AddSwaggerGen(c => {
c.SwaggerDoc("V1", new OpenApiInfo {
Version = "V1",
Title = "API",
Description = "API"
});
c.AddSecurityDefinition("api_key", securityScheme);
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
securityScheme,
new [] {
"readAccess",
"writeAccess"
}
}
});
c.EnableAnnotations();
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
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我想我遗漏了一些东西,我试着研究过https://github.com/microsoft/OpenAPI.NET/tree/master/src/Microsoft.OpenApi/Models类,但到目前为止我没有运气。可能是很小的细节,但到目前为止还没能理解.
发布于 2019-09-27 15:13:29
在使用AddSecurityRequirement时,您需要引用该方案,因为您是在全局应用它。此外,当使用"oauth2“以外的任何其他类型时,作用域数组必须为空。
下面的示例应该有效。
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Id = "api_key",
Type = ReferenceType.SecurityScheme
}
},
new List<string>() }
});发布于 2019-11-05 08:43:23
除了执行@Pavlos所说的操作之外,还需要使用AddAuthentication(string defaultScheme)来指定默认的defaultScheme。例如:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)或在authorizeData方法中指定RequireAuthorization。
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers()
.RequireAuthorization(new AuthorizeAttribute() { AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme });
});您可以使用多个AuthenticationSchemes,用逗号连接它们。
https://stackoverflow.com/questions/56980550
复制相似问题