首页 > 编程语言 >c# webapi 在swagger里添加全局过滤器

c# webapi 在swagger里添加全局过滤器

时间:2023-11-10 11:23:54浏览次数:37  
标签:webapi c# Filter context cs new actionContext swagger public

Swagger原理

  Swagger就是利用反射技术遍历所有Api接口,并且从xml文件中读取注释,在利用Swagger内置的模板组合html显示至客户端实现接口可视化,并且可调用。

在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理。引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想,通过Filter能统一地对一些通用逻辑进行处理,如:权限校验、参数加解密、参数校验等方面我们都可以利用这一特性进行统一处理,今天我们来介绍Filter的开发、使用以及讨论他们的执行顺序。

Filter的开发和调用

在默认的WebApi中,框架提供了三种Filter,他们的功能和运行条件如下表所示:

Filter 类型实现的接口描述
Authorization IAuthorizationFilter 最先运行的Filter,被用作请求权限校验
Action IActionFilter 在Action运行的前、后运行
Exception IExceptionFilter 当异常发生的时候运行

添加注册全局Filter的方法

1.创建一个ApiAuthorizationFilterAttribute.cs和ApiExceptionFilterAttribute.cs两个文件:

ApiAuthorizationFilterAttribute.cs文件如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class ApiAuthorizationFilterAttribute : AuthorizationFilterAttribute {     public override void OnAuthorization(HttpActionContext actionContext)     {           //如果用户方位的Action带有AllowAnonymousAttribute,则不进行授权验证         if (actionContext.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any())         {             return;         }           var authorization = actionContext.Request.Headers.Authorization;           if (authorization == null || authorization.Scheme != "Bearer")         {             actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, new { status = "failed", message = "token不正确!!!" });         }         else         {             string username;               var token = authorization.Parameter;       //以下就是验证token的方法,可以自己写方法进行验证啦             if (!ValidateToken(token, out username))             {                 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, new { status = "failed", message = "token不正确!!!" });             }         }     } }

  ApiExceptionFilterAttribute.cs如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 /// <summary> /// 捕捉异常 /// </summary> public class ApiExceptionFilterAttribute : ExceptionFilterAttribute {     /// <summary>     /// 重写基类的异常处理方法     /// </summary>     /// <param name="context"></param>     public override void OnException(HttpActionExecutedContext context)     {         //context.Exception.StackTrace         //1.异常日志记录         LogHelper.Error(context.Exception.ToString());           //2.返回调用方具体的异常信息         if (context.Exception is NotImplementedException)         {             context.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);         }         else if (context.Exception is TimeoutException)         {             context.Response = new HttpResponseMessage(HttpStatusCode.RequestTimeout);         }         else         {             //这里可以根据项目需要返回到客户端特定的状态码。如果找不到相应的异常,统一返回服务端错误500             context.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);         }           base.OnException(context);       }       private JsonMediaTypeFormatter _JsonFormatter;     private JsonMediaTypeFormatter JsonFormatter     {         get         {             if (_JsonFormatter == null)             {                 _JsonFormatter = new JsonMediaTypeFormatter();                 _JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();             }             return _JsonFormatter;         }     } }

2.在WebApiConfig.cs文件中添加”注册全局Filter“的那两行

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public static class WebApiConfig {     public static void Register(HttpConfiguration config)     {         // Web API 配置和服务           // Web API 路由         config.MapHttpAttributeRoutes();           //注册全局Filter         config.Filters.Add(new ApiAuthorizationFilterAttribute());         config.Filters.Add(new ApiExceptionFilterAttribute());           config.Routes.MapHttpRoute(             name: "DefaultApi",             routeTemplate: "api/{controller}/{id}",             defaults: new { id = RouteParameter.Optional }         );           log4net.Config.XmlConfigurator.Configure();       } }

3.进行swagger的相关配置,新建一个HttpHeaderFilter.cs文件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class HttpHeaderFilter : IOperationFilter {     public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)     {         if (operation.parameters == null)         {             operation.parameters = new List<Parameter>();         }           var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline(); //判断是否添加权限过滤器         var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Instance).Any(filter => filter is ApiAuthorizationFilterAttribute); //判断是否需要经过验证         var allowAnonymous = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();//判断是否否允许匿名方法         if (isAuthorized && !allowAnonymous)         {             operation.parameters.Add(new Parameter { name = "Authorization", @in "header", description = "Token", required = false, type = "string" });         }     } }

4.在文件SwaggerConfig.cs文件中找到Register方法,GlobalConfiguration.Configuration.EnableSwagger中添加:

1 c.OperationFilter<HttpHeaderFilter>();

  大功告成,如下图:

 

标签:webapi,c#,Filter,context,cs,new,actionContext,swagger,public
From: https://www.cnblogs.com/DoNetCShap/p/17823650.html

相关文章

  • containerd 基础
    containerd介绍containered原是Docker内部开发使用的运行时,后来在2017年3月捐给了NativeComputingFoundation(CNCF)。containerd是一个行业标准的容器运行时,强调简单性、稳健性和可移植性。它可作为Linux和Windows的守护进程,可以管理其主机系统的完整容器生命周期:图像传输......
  • Docsify 顶部的导航是如何配置
    如下图,我们在Docsify的文档中配置了一个顶部导航。  下面的步骤对顶部导航的配置进行简要介绍。配置有2个地方需要这个地方进行配置。首先需要在index.html文件中的 loadNavbar:true, 配置上。然后再在项目中添加一个 _navbar.md 文件。在这个文件中对导航......
  • EF报错:Unable to create an object of type 'XXXXXXX'. For the different patterns s
    这个是在EF迁移的时候报错: 解决方案:修改你的MyDbcontext:  代码如下:publicclassStoreDbContexttFactory:IDesignTimeDbContextFactory<‘你的类名’>{public‘你的类名’CreateDbContext(string[]args){varoptionsBuilder=newDbContextOptionsBuilder<‘......
  • 通过WebRTC简单实现媒体共享
    通过WebRTC简单实现媒体共享媒体协商在设置本地描述符(offer/answer)前,我们总是需要将媒体添加到连接中,只有这样在描述符中才能包含需要共享的媒体信息,除非你不需要共享媒体。在实际应用中,我们通常没办法让两个客户端直接通信,进行媒体协商。因此我们通常需要一个双方都可以访问......
  • Oracle集群RAC DG日常检查指令
    目录操作系统进程检查Pmon检查负载检查数据库检查查看数据库打开状态和相关信息查找主库判断集群正常与否判断会话等待查看连接数并与数据库配置对比判断集群和DG状态RACDG操作系统进程检查Pmon检查pmon(ProcessMonitorprocess)用于监控其他后台进程。负责在连接出现异常中止......
  • Cefsharp开发相关注意
    在非Selfhost模式下,AnyCPU,需要将Cefsharp.dll复制一份到X86/X64目录下,和CefSharp.Core.Runtime.dll同一目录,不然会异常退出。在Selfhsot下,设置了settings.BrowserSubprocessPath=System.IO.Path.GetFullPath("Example.exe");则不会出现以上情况。......
  • 11月10日css盒子模型的margin和padding属性
    目录css盒子模型margin属性如何用margin来控制其上下左右的距离margin的缩写padding属性首先它如何进行上右下左的移动现在我有一个需求就是将内容在边框的正中央显示然后就是简写的方式css盒子模型有四个属性属性描述margin用于控制元素与元素之间的距离;margin的最......
  • The Measures of Ocean Trash
    OceanTrashSolutions:7ThingsYouCanDoToday   Everyonecandosomethingtohelpsolvetheplasticpollutionproblem,andmillionsofpeopleworldwidearealreadytakingactiontoreducetheirplasticuse.Herearesevenwaysyoucanmakeadiffer......
  • Spring BeanUtils.copyProperties简化写法
    代码importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importorg.springframework.beans.BeanUtils;importorg.springframework.beans.BeansException;importorg.springframework.util.StopWatch;publicclassBeanUtils2{......
  • c# webapi swagger Area 多级层次分组 添加header参数
    nuget安装Swashbuckle安装完成后会在App_Start中生成SwaggerConfig.cs 项目右键属性生成xml文件 在SwaggerConfig中的Register中进行配置//在内部的GlobalConfiguration.Configuration.EnableSwagger中进行配置c.SingleApiVersion("v1","API");varbaseDiretory=S......