IDocumentFilter 用于隐藏/显示来自 swagger 文档的未经授权的 api

Jay*_*Jay 5 swagger swashbuckle asp.net-core

在 swashbuckle 2.2.0(带有 swagger 2.0)中,授权后响应位于弹出窗口上,这与 1.1.0(以前是页面加载)不同

如果上下文用户身份验证未经过身份验证,我们会进行类似隐藏/显示未经授权的 api 的操作,反之亦然。

Github Swashbuckle 问题 希望有人能找到解决方案或已经尝试过。

不会发送身份验证上下文来重新加载踢出 IdocumentFilter 的页面。

public class HideUnauthorizedOperations : IDocumentFilter
{
    private IHttpContextAccessor _httpContext;

    public HideUnauthorizedOperations(IHttpContextAccessor httpContext)
    {
        _httpContext = httpContext;
    }

    public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
    {

        var authenticated = _httpContext.HttpContext.User.Identity.IsAuthenticated;
        //TODO context user is null, need to fix the authentication
        //if (!authenticated)
        //{
        //    foreach (var values in swaggerDoc.Paths.Values)
        //    {
        //        var nullifyThese = new List<Operation>();
        //        foreach (var operation in values.EnumerateOperations())
        //        {
        //            if (operation != null && operation.Security?.Count > 0)
        //                nullifyThese.Add(operation);
        //        }
        //        values.NullifyOperations(nullifyThese);
        //    }
        //}
    }

}
Run Code Online (Sandbox Code Playgroud)