小编epa*_*sis的帖子

Asp.NET Core 2.2:Swagger 端点特定的安全定义

我在我的 .Net Core 2.2 REST 项目之一中使用 Swashbuckle.AspNetCore 5.0.0-rc2。在我的项目中,我提供两个不同的 api,它们在逻辑上相互连接。

今天,我设法将我的 swagger 文档分开,使每个 api 有一个 swagger 端点,仅包含相应的 api 控制器。

我设法通过将指定的组名添加到控制器的 api 资源管理器设置来做到这一点:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiExplorerSettings(GroupName = "contracts")]
public class ContractsController : BaseController

[Authorize(AuthenticationSchemes = "BasicAuthentication")]
[ApiExplorerSettings(GroupName = "clearing")]
public class ClearingController : BaseController
Run Code Online (Sandbox Code Playgroud)

使用该设置,我能够在 Startup.cs 中为 swagger 指定不同的端点

  // Enable documentation middleware
  app.UseSwagger(so =>
  {
    so.RouteTemplate = "api/doc/{documentName}/swagger.json";
  });
  app.UseSwaggerUI(suo =>
  {
    suo.SwaggerEndpoint("/api/doc/contracts/swagger.json", "Contracts API");
    suo.SwaggerEndpoint("/api/doc/clearing/swagger.json", "Clearing API");
    suo.RoutePrefix = "api/doc";
    suo.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Post, SubmitMethod.Patch, SubmitMethod.Delete);
  });
Run Code Online (Sandbox Code Playgroud)

那行得通,一切都很好。

现在您可能已经注意到,我对每个 api 的控制器使用了不同的授权方法。第一个,合约 …

c# swagger swagger-ui asp.net-core-2.2

3
推荐指数
1
解决办法
3367
查看次数

标签 统计

asp.net-core-2.2 ×1

c# ×1

swagger ×1

swagger-ui ×1