/// <summary> /// 验证微信签名,请求是否来源于微信 /// </summary> /// <returns></returns> private bool checkSignature(signatureM model) { string token = Pub.token; string[] ArrTmp = { token, model.timestamp, model.nonce }; Array.Sort(ArrTmp); string tmpStr = string.Join("", ArrTmp); string signature = ""; try { byte[] cleanBytes = Encoding.ASCII.GetBytes(tmpStr); byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes); signature = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); } catch (Exception) { } if (signature == model.signature) { return(true); } else { return(false); } }
/// <summary> ///响应微信接口测试 /// </summary> private void responseWeChat(signatureM model) { string echoStr = model.echostr; if (checkSignature(model)) { if (!string.IsNullOrWhiteSpace(echoStr)) { Response.Write(echoStr); Response.End(); } } }
public ActionResult Main(signatureM model) { responseWeChat(model); return(View()); }