Jam*_*ano 0 asp.net-web-api swagger swagger-ui asp.net-core
netcore 2.2 web api 通过使用 swagger 有什么方法可以保护我的 api 文档,然后用户才能看到他们将转到登录> 然后 api 文档的那些 api 文档。
更强大的stackoverflow。希望任何人都可以帮助..
如果用户希望访问 swagger 索引页面时未通过身份验证,您可以创建一个自定义中间件以重定向到登录:
public class SwaggerAuthorizedMiddleware
{
private readonly RequestDelegate _next;
public SwaggerAuthorizedMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
//Add your condition
if (context.Request.Path.StartsWithSegments("/swagger")
&& !context.User.Identity.IsAuthenticated)
{
context.Response.Redirect("/Identity/Account/Login");
await Task.CompletedTask;
}
else
{
await _next.Invoke(context);
}
}
}
public static class SwaggerAuthorizeExtensions
{
public static IApplicationBuilder UseSwaggerAuthorized(this IApplicationBuilder builder)
{
return builder.UseMiddleware<SwaggerAuthorizedMiddleware>();
}
}
Run Code Online (Sandbox Code Playgroud)
在启动配置中:
app.UseAuthentication();
app.UseSwaggerAuthorized();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1388 次 |
| 最近记录: |