public ActionResult Handler(string domainId, string signature, string timestamp, string nonce) { //请求的URL如下格式: //http://wxctest.zkebao.com/WeixinApi/Handler/F6AAD430-CA1F-4AFD-B2B0-6E0D2FABB622 //?signature=84001ea92e2f369642e861d557b9f4c6781db1ca //×tamp=1446393828 //&nonce=1291578710 //&encrypt_type=aes //&msg_signature=3ed4a96dbc50d491664ec3f425eb7fc1f088ac9b Guid domainGuid = Guid.Empty; if (Guid.TryParse(domainId, out domainGuid) == false) { return(new HttpStatusCodeResult(404)); } ClientDomainContext domainContext = _domainPool.GetDomainContext(domainGuid); if (domainContext == null) { return(new HttpStatusCodeResult(404)); } XMLMessageUrlParameter parameter = new XMLMessageUrlParameter(); parameter.Signature = signature; parameter.Timestamp = timestamp; parameter.Nonce = nonce; //消息加解密 //http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html //url上无encrypt_type参数或者其值为raw时表示为不加密;encrypt_type为aes时,表示aes加密(暂时只有raw和aes两种值)。 //msg_signature:表示对消息体的签名。 parameter.Encrypt_type = Request.QueryString["encrypt_type"]; parameter.Msg_signature = Request.QueryString["msg_signature"]; string resultContent = null; string postString; using (Stream stream = HttpContext.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (int)stream.Length); postString = Encoding.UTF8.GetString(postBytes); //测试用,接收微信过来的消息不能UrlDecode,会改变内容导致解密失败 //postString = Server.UrlDecode(postString); //_log.Write("Handler:\r\n" + postString); resultContent = domainContext.Handle(parameter, postString); } return(new ContentResult() { Content = resultContent }); }
public ActionResult Handler(string domainId, string signature, string timestamp, string nonce, string encrypt_type, string msg_signature) { string appId = domainId; //请求的URL如下格式: //http://wxctest.shengxunwei.com/WeixinApi/Handler/F6AAD430-CA1F-4AFD-B2B0-6E0D2FABB622 //?signature=84001ea92e2f369642e861d557b9f4c6781db1ca //×tamp=1446393828 //&nonce=1291578710 //&encrypt_type=aes //&msg_signature=3ed4a96dbc50d491664ec3f425eb7fc1f088ac9b if (String.IsNullOrEmpty(appId)) { return(new HttpStatusCodeResult(404)); } XMLMessageUrlParameter parameter = new XMLMessageUrlParameter(); parameter.Signature = signature; parameter.Timestamp = timestamp; parameter.Nonce = nonce; //消息加解密 //http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html //url上无encrypt_type参数或者其值为raw时表示为不加密;encrypt_type为aes时,表示aes加密(暂时只有raw和aes两种值)。 //msg_signature:表示对消息体的签名。 parameter.Encrypt_type = encrypt_type; parameter.Msg_signature = msg_signature; string resultContent = null; string postString; using (Stream stream = HttpContext.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (int)stream.Length); postString = Encoding.UTF8.GetString(postBytes); //测试用,接收微信过来的消息不能UrlDecode,会改变内容导致解密失败 //postString = Server.UrlDecode(postString); //_log.Write("Handler:\r\n" + postString); //resultContent = domainContext.Handle(parameter, postString); } _log.Write("收到消息推送(密文)", postString, TraceEventType.Verbose); string message = String.Empty; int decryptResult = _msgCrypt.DecryptMsg(parameter.Msg_signature, parameter.Timestamp, parameter.Nonce, postString, ref message); _log.Write("收到消息推送(明文)", message, TraceEventType.Verbose); string resultMessage = null; //全网发布时的自动化测试的专用测试公众号 if (appId == TestApp.AppId) { _log.Write("收到接入检测消息", message, TraceEventType.Verbose); resultMessage = TestApp.Handle(message); _log.Write("返回接入检测消息", resultMessage, TraceEventType.Verbose); } else { Guid?guidDomainId = _domainPool.GetDomainId(appId); if (guidDomainId == null) { return(new HttpStatusCodeResult(404)); } ClientDomainContext domainContext = _domainPool.GetDomainContext(guidDomainId.Value); if (domainContext == null) { return(new HttpStatusCodeResult(404)); } resultMessage = domainContext.Handle(message); } _log.Write("返回消息(明文)", resultMessage, TraceEventType.Verbose); if (String.IsNullOrEmpty(resultMessage) == false) { _msgCrypt.EncryptMsg(resultMessage, parameter.Timestamp, parameter.Nonce, ref resultContent); } _log.Write("返回消息(密文)", resultContent, TraceEventType.Verbose); return(new ContentResult() { Content = resultContent }); }