public OperateStatus Main(Account currentAccount, string signature, string timestamp, string nonce, string echostr) { WXBizMsgCryptHelper wxcrptyHelper = new WXBizMsgCryptHelper(); if (!string.IsNullOrEmpty(echostr)) { return VerifyUrl(currentAccount, signature, timestamp, nonce, echostr); } var content = GetContent(); FileLogHelper.WriteInfo("Content:" + content, "CorpInfoLog"); if (string.IsNullOrEmpty(content)) { return new OperateStatus { ResultSign = ResultSign.Failed, Message = "请求参数内容不存在" }; } string deContent; var ret = wxcrptyHelper.DecryptMsg(currentAccount, signature, timestamp, nonce, content, out deContent); if (ret != EnumWXBizMsgCryptErrorCode.OK) { return new OperateStatus { ResultSign = ResultSign.Failed, Message = "解密不通过," + ret }; } FileLogHelper.WriteInfo("DeCentent:" + deContent, "CorpInfoLog"); var dicParams = XmlHelper.ConvertToDictionary(deContent); FileLogHelper.WriteInfo("DicParams:" + dicParams, "CorpInfoLog"); if (!dicParams.ContainsKey("MsgType")) { return new OperateStatus { ResultSign = ResultSign.Failed, Message = "不包含MsgType" }; } string responseStr; switch (dicParams["MsgType"]) { case "text": responseStr = GetResponseForText(currentAccount, dicParams); break; case "event": responseStr = GetResponseForEvent(currentAccount, dicParams); break; default: responseStr = "暂时不支持"; break; } FileLogHelper.WriteInfo("ResponseString:" + responseStr, "CorpInfoLog"); string enResponseStr; ret = wxcrptyHelper.EncryptMsg(currentAccount, responseStr, timestamp, nonce, out enResponseStr); if (ret != EnumWXBizMsgCryptErrorCode.OK) { return new OperateStatus { ResultSign = ResultSign.Failed, Message = "加密不通过" }; } FileLogHelper.WriteInfo("EncryptMsg:" + enResponseStr, "CorpInfoLog"); return new OperateStatus { ResultSign = ResultSign.Success, ReturnValue = enResponseStr }; }
private OperateStatus VerifyUrl(Account currentAccount, string signature, string timestamp, string nonce, string echostr) { WXBizMsgCryptHelper wxcrptyHelper = new WXBizMsgCryptHelper(); string replyEchoStr; var ret = wxcrptyHelper.VerifyURL(currentAccount, signature, timestamp, nonce, echostr, out replyEchoStr); if (ret != EnumWXBizMsgCryptErrorCode.OK) { return new OperateStatus { ResultSign = ResultSign.Failed, Message = "验签不通过" }; } return new OperateStatus { ResultSign = ResultSign.Success, ReturnValue = replyEchoStr }; }