zoukankan      html  css  js  c++  java
  • WebAPI 自定义过滤

    自定义filter 类过滤

    -------------------------------------------------------------------------

    public class MyFilter : IAuthorizationFilter
    {
      public bool AllowMultiple => true;

      public async Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>>     continuation)
      {
        IEnumerable<string> name;
        bool v = actionContext.Request.Headers.TryGetValues("username", out name);

        string nm = name.FirstOrDefault();
        if(nm==null)
        {
          return new HttpResponseMessage(HttpStatusCode.Unauthorized);
      }
      else
      {
        return await continuation();
      }

      return new HttpResponseMessage(HttpStatusCode.Unauthorized);

      }
    }

    添加filter 类到配置文件

    ---------------------------------------------------------------------------------------------------

    public static class WebApiConfig
    {
      public static void Register(HttpConfiguration config)
      {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional });
        config.Filters.Add(new MyFilter());
      }
    }

  • 相关阅读:
    apache伪静态设置
    ZeroClipboard.js兼容各种浏览器复制到剪切板上
    table 如何给tr border颜色
    JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
    jquery操作select(增加,删除,清空)
    JS生成随机的由字母数字组合的字符串
    Redis连接(二)
    Redis集群(一)
    wap启用宏
    windows 10激活
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14032260.html
Copyright © 2011-2022 走看看