架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 17418|回复: 0

[ASP.NET] .NET Web API之ActionFilterAttribute 过滤器使用

[复制链接]
发表于 2016-9-20 08:56:16 | 显示全部楼层 |阅读模式
  1. public class Auth : ActionFilterAttribute
  2.     {
  3.         private const string Origin = "Origin";
  4.         private const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
  5.         private const string originHeaderdefault = "*";

  6.         /// <summary>
  7.         /// 走完api之后
  8.         /// </summary>
  9.         /// <param name="actionExecutedContext"></param>
  10.         public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
  11.         {
  12.             actionExecutedContext.Response.Headers.Add(AccessControlAllowOrigin, originHeaderdefault);
  13.         }

  14.         /// <summary>
  15.         /// 进api之前
  16.         /// </summary>
  17.         /// <param name="actionContext"></param>
  18.         public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
  19.         {
  20.             base.OnActionExecuting(actionContext);
  21.             //先判断时间戳
  22.             long time = long.Parse(actionContext.ActionArguments["timestamp"].ToString());
  23.             bool result = Tools.TimeHelp.IsTime(time, Convert.ToDouble(ConfigurationManager.AppSettings["TimestampInterval"]));
  24.             if (!result)
  25.             {
  26.                 //时间戳不正确
  27.                 Models.ClientModel model = new Models.ClientModel
  28.                 {
  29.                     result = false,
  30.                     message = "Time is not right!",
  31.                     code = 200,
  32.                     data = null
  33.                 };
  34.                 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, model);
  35.                 return;
  36.             }
  37.             Dictionary<string, object> dic = new Dictionary<string, object>();
  38.             foreach (var item in actionContext.ActionArguments)
  39.             {
  40.                 dic.Add(item.Key, item.Value);
  41.             }
  42.             //移除post的key
  43.             if (dic.ContainsKey("postData"))
  44.             {
  45.                 dic.Remove("postData");
  46.             }
  47.             if (dic.ContainsKey("signature"))
  48.             {
  49.                 dic.Remove("signature");
  50.             }
  51.             //肯定能取到client
  52.             string client = dic["client"].ToString();
  53.             //得到secretkey
  54.             string secretkey = string.Empty;
  55.             if (!Models.Config.DIC_SUPPLIER.ContainsKey(client))
  56.             {
  57.                 //没有找到客户
  58.                 Models.ClientModel model = new Models.ClientModel
  59.                 {
  60.                     result = false,
  61.                     message = "Can't find Client!",
  62.                     code = 200,
  63.                     data = null
  64.                 };
  65.                 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, model);
  66.                 return;
  67.             }
  68.             else {
  69.                 secretkey = Models.Config.DIC_SUPPLIER[client];
  70.             }
  71.             //sign签名
  72.             string sign = Tools.SignHelper.Sign(secretkey, dic);
  73.             string signature = actionContext.ActionArguments["signature"].ToString();
  74.             if (!sign.Equals(signature))
  75.             {
  76.                 //签名不正确
  77.                 Models.ClientModel model = new Models.ClientModel
  78.                 {
  79.                     result = false,
  80.                     message = "The signature is not correct!",
  81.                     code = 200,
  82.                     data = null
  83.                 };
  84.                 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, model);
  85.                 return;
  86.             }
  87.         }
  88.     }
复制代码
生产环境的代码。




上一篇:阿里云识别身份证/验证码/人脸年龄识别[附源码]
下一篇:微信禁止在safari打开网页
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-9-22 06:56

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表