在Swagger的Swashbuckler实现中添加Authorize属性过滤器

在Swagger的Swashbuckler实现中添加Authorize属性过滤器,swagger,swashbuckle,swashbuckle.examples,Swagger,Swashbuckle,Swashbuckle.examples,希望在Swashback的Swagger实现中将AuthorizeFilterAttribute或AnonymousFilterAttribute添加到端点,以便我可以看到在以/Swagger结尾的运行webapi中生成的文档文件中的每个端点上使用了哪个属性。现在可能吗 我特别想添加一个大的粗体标签,上面写着这个端点是[匿名的],或者这个端点正在使用[授权],并且让它们看起来与摘要或备注文本不同 此外,我希望能够过滤掉每个端点的所有不同类型的限制过滤器属性,包括[NoAction]、[Autho

希望在Swashback的Swagger实现中将AuthorizeFilterAttribute或AnonymousFilterAttribute添加到端点,以便我可以看到在以/Swagger结尾的运行webapi中生成的文档文件中的每个端点上使用了哪个属性。现在可能吗

我特别想添加一个大的粗体标签,上面写着这个端点是[匿名的],或者这个端点正在使用[授权],并且让它们看起来与摘要或备注文本不同

此外,我希望能够过滤掉每个端点的所有不同类型的限制过滤器属性,包括[NoAction]、[Authorize]和[Anonymous],其中一个可能位于每个控制器端点的顶部。甚至可能最终在每个端点上添加除这些之外的其他类型的FilterAttribute

目前看起来只有HTTP方法、请求和响应对象可以在当前的实现中检索,因此我无法找到关于这方面的最终信息

由于这是一个虚张声势的实现,这些.NET特定的属性过滤器是否不转换为虚张声势的b/c?它们只实现虚张声势规范中的内容,而不实现其他内容

最后,是他们对Swashback实现的.NET特定扩展实现了这一点吗


谢谢

对于向未受保护的方法/操作添加标签的部件,可以使用如下操作筛选器

  public class UnprotectedOperationFilter : IOperationFilter
  {

    private bool HasAttribute(MethodInfo methodInfo, Type type, bool inherit)
    {
      // inhertit = true also checks inherited attributes
      var actionAttributes = methodInfo.GetCustomAttributes(inherit);
      var controllerAttributes = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(inherit);
      var actionAndControllerAttributes = actionAttributes.Union(controllerAttributes);

      return actionAndControllerAttributes.Any(attr => attr.GetType() == type);
    }

    public void Apply(Operation operation, OperationFilterContext context)
    {

      bool hasAuthorizeAttribute = HasAttribute(context.MethodInfo, typeof(AuthorizeAttribute), true);
      bool hasAnonymousAttribute = HasAttribute(context.MethodInfo, typeof(AllowAnonymousAttribute), true);

      // so far as I understood the action/operation is public/unprotected 
      // if there is no authorize or an allow anonymous (allow anonymous overrides all authorize)
      bool isAuthorized = hasAuthorizeAttribute && !hasAnonymousAttribute;

      if (!isAuthorized)
      {
        operation.Description = 
          "<p><bold>BIG BOLD LABEL indicating an UPROTECTED PUBLIC method</bold></p>" 
          + operation.Description;
      }

    }
  }
公共类UnprotectedOperationFilter:IOperationFilter
{
私有布尔HasAttribute(MethodInfo MethodInfo,类型类型,布尔继承)
{
//inherit=true还检查继承的属性
var actionAttributes=methodInfo.GetCustomAttributes(继承);
var controllerAttributes=methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(继承);
var actionAndControllerAttributes=actionAttributes.Union(controllerAttributes);
返回actionAndControllerAttributes.Any(attr=>attr.GetType()==type);
}
公共无效应用(操作,操作筛选器上下文)
{
bool hasaauthorizeAttribute=hasaAttribute(context.MethodInfo,typeof(AuthorizeAttribute),true);
bool hasAnonymousAttribute=HasAttribute(context.MethodInfo,typeof(AllowAnonymousAttribute),true);
//据我所知,该行动/操作是公开的/不受保护的
//如果没有授权或允许匿名(允许匿名覆盖所有授权)
bool isAuthorized=hasAuthorizeAttribute&!hasAnonymousAttribute;
如果(!未授权)
{
操作说明=
“大粗体标签,表示受保护的公共方法

” +操作说明; } } }
加上

services.AddSwaggerGen(c => { c.OperationFilter<UnprotectedOperationFilter>();} );
services.AddSwaggerGen(c=>{c.OperationFilter();});

我不明白过滤不同属性是什么意思,但我希望上面的代码可以帮助您检查属性是否存在,并执行您希望执行的操作。

发现:我注意到,当您使用/swagger打开服务时,您可以直接在右上角的swashbuckler搜索框中添加承载令牌。