.net core 如何使用Windows身份验证保护Swagger UI

.net core 如何使用Windows身份验证保护Swagger UI,.net-core,swagger,windows-authentication,.net Core,Swagger,Windows Authentication,我们有一个.NETCore2.2WebAPI,它使用SwiggerUI公开WebAPI定义。我们希望仅将此端点保护给特定广告组内的用户。我们目前正在使用Windows和匿名身份验证。问题是,我们不能强制施展招摇过市的手段,使用Windows身份验证来阻止用户 有什么想法吗?尽管令人沮丧,但到目前为止,我发现保护Swagger端点(通过Swashback)最简单的方法就是将其置于自己的路径下,然后使用一个简单的中间件来验证授权状态,就像在提供授权之前一样。这是为NETCore3.1编写的,用于检查

我们有一个.NETCore2.2WebAPI,它使用SwiggerUI公开WebAPI定义。我们希望仅将此端点保护给特定广告组内的用户。我们目前正在使用Windows和匿名身份验证。问题是,我们不能强制施展招摇过市的手段,使用Windows身份验证来阻止用户


有什么想法吗?

尽管令人沮丧,但到目前为止,我发现保护Swagger端点(通过Swashback)最简单的方法就是将其置于自己的路径下,然后使用一个简单的中间件来验证授权状态,就像在提供授权之前一样。这是为NETCore3.1编写的,用于检查索赔,因此您可能需要根据您的场景调整授权检查。显然,您仍然需要/希望对it文档的端点进行授权,但您不一定希望每个最终用户在任何情况下都能访问文档

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

/// <summary>
/// Middleware to protect API Swagger docs
/// </summary>
public class SwaggerAuthorizationMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger _logger;

    public SwaggerAuthorizationMiddleware(RequestDelegate next, ILogger<SwaggerAuthorizationMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task Invoke(HttpContext context)
    {
        // If API documentation route and user isn't authenticated or doesn't have the appropriate authorization, then block
        if (context.Request.Path.StartsWithSegments("/apidoc"))
            && (!context.User.Identity.IsAuthenticated || !context.User.HasClaim("ClaimName", "ClaimValue")))
        {
            _logger.LogWarning($"API documentation endpoint unauthorized access attempt by [{context.Connection.RemoteIpAddress}]");
            context.Response.StatusCode = StatusCodes.Status401Unauthorized;
            return;
        }

        await _next.Invoke(context);
    }
}
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用System.Threading.Tasks;
/// 
///保护API招摇过市文档的中间件
/// 
公共类虚张声势中间件
{
private readonly RequestDelegate\u next;
专用只读ILogger\u记录器;
公共SwaggerAuthorizationMiddleware(RequestDelegate下一步,ILogger记录器)
{
_下一个=下一个;
_记录器=记录器;
}
公共异步任务调用(HttpContext上下文)
{
//如果API文档路由和用户未经过身份验证或没有适当的授权,则阻止
if(context.Request.Path.StartsWithSegments(“/apidoc”))
&&(!context.User.Identity.IsAuthenticated | | |!context.User.HasClaim(“ClaimName”、“ClaimValue”))
{
_logger.LogWarning($“API文档端点[{context.Connection.RemoteIpAddress}]未经授权的访问尝试”);
context.Response.StatusCode=StatusCodes.status401未授权;
返回;
}
wait_next.Invoke(上下文);
}
}
启动期间:

app.UseAuthorization(); // before the middleware
app.UseMiddleware<SwaggerAuthorizationMiddleware>();
app.UseSwagger(c =>
{
    c.RouteTemplate = "apidoc/swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/apidoc/swagger/v1/swagger.json", "My Service");
    c.RoutePrefix = "apidoc";
});
app.UseAuthorization();//中间件之前
app.UseMiddleware();
app.UseSwagger(c=>
{
c、 RouteTemplate=“apidoc/swagger/{documentName}/swagger.json”;
});
app.UseSwaggerUI(c=>
{
c、 SwaggerEndpoint(“/apidoc/swagger/v1/swagger.json”,“我的服务”);
c、 RoutePrefix=“apidoc”;
});

我们尝试了非常类似的方法。问题是,因为我们同时使用windows和匿名身份验证,当他们导航到swagger时,身份验证模式是匿名的,因此我们无法检查他们的角色。您可以使用
context.Request.Path.StartsWithSegments(“/apidoc”)&&!context.User.Identity.IsAuthenticated
作为约束,然后触发
等待context.ChallengeAsync(“Windows”)
强制质询?似乎可以通过将浏览器指向
//apidoc
而不是
/apidoc
来绕过此问题。