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);
            }
    
        }
  • 相关阅读:
    植物:吊兰
    植物:文竹
    理论:混沌理论
    图书-数学:《数学之美》
    调味品:味精
    linux查看文件大小df-du
    linux防火墙开启-关闭
    tomcat端口号、日志、启停
    chmod
    jdk-tomcat环境变量设置
  • 原文地址:https://www.cnblogs.com/zhuyapeng/p/8384140.html
Copyright © 2011-2022 走看看