首页 > Windows开发 > 详细

webAPI过滤器添加参数签名

时间:2018-01-30 13:52:34      阅读:400      评论:0      收藏:0      [点我收藏+]

项目需求:

  接口对安卓和IOS开发接口,需要房子用户窜改数据请求接口。添加sign签名校验参数。

代码如下:加上特性标签就可以控制部分接口验证

 public class SignAuthorizeFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext filterContext)
        {

            var actionList = filterContext.ActionDescriptor.GetCustomAttributes<EncryptDataAttribute>();
            var controllList = filterContext.ControllerContext.ControllerDescriptor.GetCustomAttributes<EncryptDataAttribute>();

            if (actionList.Any()|| controllList.Any())
            {
                string key = ConfigSection.Get("Key");
                if (!string.IsNullOrWhiteSpace(key))
                {
                    var result = new AjaxResCode();
                    //1.验证入参
                    string token = HttpContext.Current.Request.Params["token"];
                    string appkey = HttpContext.Current.Request.Params["appkey"];
                    string timestamp = HttpContext.Current.Request.Params["timestamp"];
                    string digest = HttpContext.Current.Request.Params["digest"];
                    string v = HttpContext.Current.Request.Params["v"];

                    if (string.IsNullOrWhiteSpace(token) ||
                        string.IsNullOrWhiteSpace(appkey) ||
                        string.IsNullOrWhiteSpace(timestamp) ||
                        string.IsNullOrWhiteSpace(digest) ||
                        string.IsNullOrWhiteSpace(v))
                    {
                        result.Message = "请求非法。。。。!";
                        result.ResultCode = (int)ResultCode.Nopermit;
                        filterContext.Response = filterContext.Request.CreateResponse(HttpStatusCode.OK, result);
                    }


                    NameValueCollection coll = HttpContext.Current.Request.Form;
                    StringBuilder paramStr = new StringBuilder();

                    var keys = new List<string>();
                    foreach (string param in coll.Keys)
                    {
                        if (!string.IsNullOrEmpty(param))
                        {
                            keys.Add(param.ToLower());
                        }
                    }

                    keys.Sort();
                    foreach (string p in keys)
                    {
                        if (p != "digest")
                        {
                            if (!string.IsNullOrEmpty(coll[p]))
                            {
                                paramStr.Append(coll[p]);
                            }
                        }
                    }
                    paramStr.Append(key);
                    if (DESEncrypt.MD5ToUpper(paramStr.ToString()) != digest)
                    {
                        result.Message = "请求非法!。。。。。";
                        result.ResultCode = (int)ResultCode.Nopermit;
                        filterContext.Response = filterContext.Request.CreateResponse(HttpStatusCode.OK, result);
                    }
                }
            }

            base.OnActionExecuting(filterContext);
        }

    }

 

webAPI过滤器添加参数签名

原文:https://www.cnblogs.com/zhuyapeng/p/8384140.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!