最近也要開發回調模式,弄了好多天,配置URL都會跳出“echostr校驗失敗,請您檢查是否正確解密並輸出明文echostr ”,昨天晚上終於找到我的問題在哪了!!今天趕緊把這口惡氣給出了,哈哈哈
下面擼問題:首先,我參考的是伍華聰老師的企業號開發系列,甩上鏈接:http://www.cnblogs.com/wuhuacong/p/3996000.html,里面講的很詳細,主要我加了一行urldecode(在微信的開發文檔——回調模式里面有寫要對獲取到的echostr進行urldecode),具體會在下面的代碼里用紅色標明。最最最重要的,就是!SAE並不能用(對於C#來說)!至少我不行!昨天看見網上說SAE不支持.net,然后測試了一下,果然真的是卡在這里了!后來用了公司的外網服務器,就成功了!!原來卡了這么久完全不是代碼問題,哭都來不及。。。下面附上代碼(親測可以順利開啟回調模式,至於消息回調還沒做,
using System;
using System.Web; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; public class Auth : IHttpHandler { public void ProcessRequest(HttpContext context) { //string postString = string.Empty; if (HttpContext.Current.Request.HttpMethod.ToUpper() != "GET") { } else { WXHD(); } } private void WXHD() { #region 獲取關鍵參數 string token = ConfigurationManager.AppSettings["CorpToken"];//從配置文件獲取Token string encodingAESKey = ConfigurationManager.AppSettings["EncodingAESKey"];//從配置文件獲取EncodingAESKey string corpId = ConfigurationManager.AppSettings["CorpId"];//從配置文件獲取corpId #endregion string echoStringinitial = HttpContext.Current.Request.QueryString["echostr"]; string echoString = HttpUtility.UrlDecode(echoStringinitial); // 開發文檔中寫的“企業在獲取時需要做urldecode處理” string signature = HttpContext.Current.Request.QueryString["msg_signature"]; string timestamp = HttpContext.Current.Request.QueryString["timestamp"]; string nonce = HttpContext.Current.Request.QueryString["nonce"]; string decryptEchoString = ""; if (CheckSignature(token, signature, timestamp, nonce, corpId, encodingAESKey, echoString, ref decryptEchoString)) { if (!string.IsNullOrEmpty(decryptEchoString)) { HttpContext.Current.Response.Write(decryptEchoString); HttpContext.Current.Response.End(); } } } public bool CheckSignature(string token, string signature, string timestamp, string nonce, string corpId, string encodingAESKey, string echostr, ref string retEchostr) { WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpId); int result = wxcpt.VerifyURL(signature, timestamp, nonce, echostr, ref retEchostr); if (result != 0) { LogTextHelper.Error("ERR: VerifyURL fail, ret: " + result); return false; } return true; //ret==0表示驗證成功,retEchostr參數表示明文,用戶需要將retEchostr作為get請求的返回參數,返回給企業號。 // HttpUtils.SetResponse(retEchostr); } public bool IsReusable { get { return false; } } }
上面是Auth.ashx(一般處理程序),其中的WXBizMsgCrypt是微信開發文檔中給的,直接下載使用就好。下載鏈接:http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8A%A0%E8%A7%A3%E5%AF%86%E5%BA%93%E4%B8%8B%E8%BD%BD%E4%B8%8E%E8%BF%94%E5%9B%9E%E7%A0%81
日志記錄的幫助類LogTextHelper我是網上找的,直接找度娘就好了,實在不行,直接把那一行注釋掉就好了,當然這樣就沒日志可以看咯。
webconfig配置文件里添加: <appSettings> <add key ="CorpToken" value="你的token"/> <add key="EncodingAESKey" value="你的encodingaeskey"/> <add key="CorpId" value="企業的corpid"/> </appSettings>
URL中記得要加上端口號,Token和EncodingAESKey我都是點的后面的隨機獲取,再坑爹的一步來了,點保存的話,你可能點了一下以后他還是會提示“echostr校驗失敗,請您檢查是否正確解密並輸出明文echostr ”,but!不要氣餒,一直點,他就會顯示回調配置成功啦!
好了,至此,回調模式開啟總算是搞定了,下面說一下我是怎么知道不是代碼問題的,在下載WXBizMsgCrypt時,壓縮包里有一份sample.cs,如果是用的SAE作為自己服務器失敗的,可以去SAE的日志中心(應用——日志中心,就在 代碼管理 的下面幾個),這樣就能看到URL和HTTP返回碼了,把里面的每個參數復制到對應的sample.cs中,記得echostr要urldecode(百度就能解碼),然后在sample.cs中斷點,看ret是不是0,如果是,那程序說明並沒有問題(記得sample.cs中的token, corpID, EncodingAESKey也要改成自己的)。
OK,我要繼續去碼消息回復加解密了,有問題請留言,說的不對的請指正喔。