webAPI过滤器添加参数签名
生活随笔
收集整理的這篇文章主要介紹了
webAPI过滤器添加参数签名
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
項(xiàng)目需求:
接口對(duì)安卓和IOS開發(fā)接口,需要房子用戶竄改數(shù)據(jù)請(qǐng)求接口。添加sign簽名校驗(yàn)參數(shù)。
代碼如下:加上特性標(biāo)簽就可以控制部分接口驗(yàn)證
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.驗(yàn)證入?yún)?/span>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 = "請(qǐng)求非法。。。。!";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 = "請(qǐng)求非法!。。。。。";result.ResultCode = (int)ResultCode.Nopermit;filterContext.Response = filterContext.Request.CreateResponse(HttpStatusCode.OK, result);}}}base.OnActionExecuting(filterContext);}}?
轉(zhuǎn)載于:https://www.cnblogs.com/zhuyapeng/p/8384140.html
總結(jié)
以上是生活随笔為你收集整理的webAPI过滤器添加参数签名的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十进制转换成二进制列表
- 下一篇: 数字货币评级