Asp.net core IDocumentFilter用于隐藏/显示来自swagger文档的未授权API
在Swashback 2.2.0(使用swagger 2.0)中,与1.1.0(以前是页面加载)不同,授权后响应在弹出窗口上 我们有一些类似于隐藏/显示未经授权的api,如果上下文用户身份验证未经身份验证,则隐藏/反之亦然 希望有人能找到解决方案,或者已经尝试过了 未发送身份验证上下文以重新加载启动IdocumentFilter的页面Asp.net core IDocumentFilter用于隐藏/显示来自swagger文档的未授权API,asp.net-core,swagger,swashbuckle,Asp.net Core,Swagger,Swashbuckle,在Swashback 2.2.0(使用swagger 2.0)中,与1.1.0(以前是页面加载)不同,授权后响应在弹出窗口上 我们有一些类似于隐藏/显示未经授权的api,如果上下文用户身份验证未经身份验证,则隐藏/反之亦然 希望有人能找到解决方案,或者已经尝试过了 未发送身份验证上下文以重新加载启动IdocumentFilter的页面 public class HideUnauthorizedOperations : IDocumentFilter { private IHttpCont
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);
// }
//}
}
}
公共类隐藏授权操作:IDocumentFilter
{
私有IHttpContextAccessor_httpContext;
公共隐藏授权操作(IHttpContextAccessor httpContext)
{
_httpContext=httpContext;
}
公共无效应用(SwaggerDocument swaggerDoc、DocumentFilterContext上下文)
{
var authenticated=\u httpContext.httpContext.User.Identity.IsAuthenticated;
//TODO上下文用户为空,需要修复身份验证
//如果(!已验证)
//{
//foreach(swaggerDoc.path.values中的变量值)
// {
//var nullifythes=新列表();
//foreach(values.EnumerateOperations()中的var操作)
// {
//if(operation!=null&&operation.Security?.Count>0)
//取消这些。添加(操作);
// }
//值。NullifyOperations(Nullifythis);
// }
//}
}
}