zoukankan      html  css  js  c++  java
  • webAPI过滤器添加参数签名

    项目需求:

      接口对安卓和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);
            }
    
        }
  • 相关阅读:
    替换所有的cell的右侧箭头
    (转载)iOS UILabel自定义行间距时获取高度
    UITableViewCell的separator分隔线设置失效
    tableview中在tableheaderView上放一个视图,第一次进入视图显示不正常,往下拉视图仍然不正常,往上拉视图正常
    Xcode打印frame id
    使用System Sound Services 播放音效(最简单,比较底层),调用AudioServicesPlaySystemSound()
    tcpdump
    /pentest/sniffers/hamster
    dsniff
    /usr/local/sbin/dsniff
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/8384140.html
Copyright © 2011-2022 走看看