首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >过渡到Swashbuckle 5.0

过渡到Swashbuckle 5.0
EN

Stack Overflow用户
提问于 2019-07-11 01:34:09
回答 2查看 549关注 0票数 3

我通过Swashbuckle V4使用Swagger,通过使用API密钥对端点进行了验证。

在使用Swashbuckle V4时,以下配置工作得很好(请注意,只显示了Swagger代码):

代码语言:javascript
复制
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配置:

代码语言:javascript
复制
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密钥进行身份验证。

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

代码语言:javascript
复制
 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类,但到目前为止我没有运气。可能是很小的细节,但到目前为止还没能理解.

EN

回答 2

Stack Overflow用户

发布于 2019-09-27 15:13:29

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

下面的示例应该有效。

代码语言:javascript
复制
c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Id = "api_key",
                                Type = ReferenceType.SecurityScheme
                            }
                        },
                        new List<string>() }
                });
票数 3
EN

Stack Overflow用户

发布于 2019-11-05 08:43:23

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

代码语言:javascript
复制
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

或在authorizeData方法中指定RequireAuthorization。

代码语言:javascript
复制
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers()
        .RequireAuthorization(new AuthorizeAttribute() { AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme });
});

您可以使用多个AuthenticationSchemes,用逗号连接它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56980550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档