/// <summary> /// 验证签名,检验是否是从微信服务器上发出的请求 /// </summary> /// <param name="model">请求参数模型 Model</param> /// <returns>是否验证通过</returns> private bool CheckSignature(WeChatRequestModel model) { string signature, timestamp, nonce, tempStr; //获取请求来的参数 signature = model.signature; timestamp = model.timestamp; nonce = model.nonce; //创建数组,将 Token, timestamp, nonce 三个参数加入数组 string[] array = { Token, timestamp, nonce }; //进行排序 Array.Sort(array); //拼接为一个字符串 tempStr = String.Join("", array); //对字符串进行 SHA1加密 SHA1 algorithm = SHA1.Create(); byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(tempStr)); string sh1 = ""; for (int i = 0; i < data.Length; i++) { sh1 += data[i].ToString("x2").ToUpperInvariant(); } tempStr = sh1.ToLower(); //判断signature 是否正确 if (tempStr.Equals(signature)) { return(true); } else { return(false); } }
public ContentResult Valid(WeChatRequestModel model) { //获取请求来的 echostr 参数 string echoStr = model.echostr; //通过验证 if (CheckSignature(model)) { if (!string.IsNullOrEmpty(echoStr)) { //将随机生成的 echostr 参数 原样输出 return(Content(echoStr)); //截止输出流 } } return(Content(string.Empty)); }