Asp.net core 如何通过登录页面保护ASP.NET Core 2.2 Web Api招摇过市?
netcore 2.2 web api通过使用swagger,在用户可以查看api文档之前,是否有任何方法可以确保我的api文档的安全?他们将进入登录>然后进入api文档Asp.net core 如何通过登录页面保护ASP.NET Core 2.2 Web Api招摇过市?,asp.net-core,asp.net-web-api,swagger,swagger-ui,Asp.net Core,Asp.net Web Api,Swagger,Swagger Ui,netcore 2.2 web api通过使用swagger,在用户可以查看api文档之前,是否有任何方法可以确保我的api文档的安全?他们将进入登录>然后进入api文档 更多的电源溢出。希望任何人都能提供帮助。您可以创建一个自定义中间件,在用户希望访问swagger索引页面时,如果用户未通过身份验证,则可以重定向到登录: public class SwaggerAuthorizedMiddleware { private readonly RequestDelegate _next;
更多的电源溢出。希望任何人都能提供帮助。您可以创建一个自定义中间件,在用户希望访问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>();
}
}
@SajadAfaghiy这一个用于令牌身份验证,而不是登录purposes@eVolve不,我的意思是登录页面不是API的授权。这帮了我很多忙。我还在Startup.cs中配置了OpenIdConnect,并使用了
wait context.ChallengeAsync()代码>而不是重定向到登录页面
app.UseAuthentication();
app.UseSwaggerAuthorized();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});