/// <summary> /// 核心执行方法 == 验证签名和消息体信息解密处理部分 /// </summary> /// <param name="recXml">消息内容</param> /// <param name="signature">微信加密签名</param> /// <param name="timestamp">时间戳</param> /// <param name="nonce">随机数</param> /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns> protected ResultMo <string> ProcessBegin(string recXml, string signature, string timestamp, string nonce) { if (string.IsNullOrEmpty(recXml)) { return(new ResultMo <string>(ResultTypes.ObjectNull, "接收的消息体为空!")); } var resCheck = WxMsgHelper.CheckSignature(m_Config.Token, signature, timestamp, nonce); if (resCheck.IsSuccess()) { if (m_Config.SecurityType != WxSecurityType.None) { XmlDocument xmlDoc = null; var dirs = WxMsgHelper.ChangXmlToDir(recXml, ref xmlDoc); if (dirs == null || !dirs.ContainsKey("Encrypt")) { return(new ResultMo <string>(ResultTypes.ObjectNull, "加密消息为空")); } var recMsgXml = Cryptography.WxAesDecrypt(dirs["Encrypt"], m_Config.EncodingAesKey); return(new ResultMo <string>(recMsgXml)); } return(new ResultMo <string>(recXml)); } return(resCheck.ConvertToResultOnly <string>()); }
/// <summary> /// 核心执行 过程的 验签和解密 /// </summary> /// <param name="recXml">消息内容</param> /// <param name="signature">微信加密签名</param> /// <param name="timestamp">时间戳</param> /// <param name="nonce">随机数</param> /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns> private ResultMo <string> PrepareExecute(string recXml, string signature, string timestamp, string nonce) { if (string.IsNullOrEmpty(recXml)) { return(new ResultMo <string>(ResultTypes.ObjectNull, "接收的消息体为空!")); } var resCheck = WxMsgHelper.CheckSignature(ApiConfig.Token, signature, timestamp, nonce); if (!resCheck.IsSuccess()) { return(resCheck.ConvertToResult <string>()); } if (ApiConfig.SecurityType == WxSecurityType.None) { return(new ResultMo <string>(recXml)); } var dirs = WxMsgHelper.ChangXmlToDir(recXml, out XmlDocument xmlDoc); if (dirs == null || !dirs.TryGetValue("Encrypt", out var encryStr) || string.IsNullOrEmpty(encryStr)) { return(new ResultMo <string>(ResultTypes.ObjectNull, "加密消息为空")); } var recMsgXml = Cryptography.WxAesDecrypt(encryStr, ApiConfig.EncodingAesKey); return(new ResultMo <string>(recMsgXml)); }
/// <summary> /// 服务器验证 /// </summary> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <param name="echostr"></param> /// <returns></returns> public ResultMo <string> CheckServerValid(string signature, string timestamp, string nonce, string echostr) { var checkSignRes = WxMsgHelper.CheckSignature(m_Config.Token, signature, timestamp, nonce); var resultRes = checkSignRes.ConvertToResultOnly <string>(); resultRes.data = resultRes.IsSuccess() ? echostr : string.Empty; return(resultRes); }