您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關C#開發(fā)微信之微信企業(yè)號消息和事件接收處理及解密的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1、企業(yè)號回調(diào)模式的設置
和公眾號一樣,微信企業(yè)號如果需要進行二次開發(fā),也是需要在后臺設置好對應的回調(diào)參數(shù),如下界面所示。
設置好這些后,檢查通過后,我們就可以在自己微信應用服務器上進行消息的收發(fā)操作了。
在回調(diào)的消息入口處,我們需要對POST數(shù)據(jù)和普通的GET數(shù)據(jù)進行分開處理,GET數(shù)據(jù)是微信自身的驗證處理,POST數(shù)據(jù)是微信消息的交互操作。
/// <summary> /// 企業(yè)號回調(diào)信息接口。統(tǒng)一接收并處理信息的入口。 /// </summary> public class corpapi : IHttpHandler { /// <summary> /// 處理企業(yè)號的信息 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) {
上面我們定義了一個一般應用處理程序來對消息進行處理。
然后我們分開不同的消息類型(POST、GET 方式),針對性的進行處理。
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } if (!string.IsNullOrEmpty(postString)) { Execute(postString, accountInfo); } } else { Auth(accountInfo); }
下面是微信對于回調(diào)模式,驗證URL的說明。
驗證URL有效性
當你提交以上信息時,企業(yè)號將發(fā)送GET請求到填寫的URL上,GET請求攜帶四個參數(shù),企業(yè)在獲取時需要做urldecode處理,否則會驗證不成功。
參數(shù) | 描述 | 是否必帶 |
---|---|---|
msg_signature | 微信加密簽名,msg_signature結合了企業(yè)填寫的token、請求中的timestamp、nonce參數(shù)、加密的消息體 | 是 |
timestamp | 時間戳 | 是 |
nonce | 隨機數(shù) | 是 |
echostr | 加密的隨機字符串,以msg_encrypt格式提供。需要解密并返回echostr明文,解密后有random、msg_len、msg、$CorpID四個字段,其中msg即為echostr明文 | 首次校驗時必帶 |
企業(yè)通過參數(shù)msg_signature對請求進行校驗,如果確認此次GET請求來自企業(yè)號,那么企業(yè)應用對echostr參數(shù)解密并原樣返回echostr明文(不能加引號),則接入驗證生效,回調(diào)模式才能開啟。
后續(xù)回調(diào)企業(yè)時都會在請求URL中帶上以上參數(shù)(echostr除外),校驗方式與首次驗證URL一致。
根據(jù)上面的說明,我們需要獲取這些參數(shù),然后調(diào)用微信提供的消息處理函數(shù)進行加解密處理。
在驗證URL的Auth(accountInfo);操作里面,我們可以看到核心的內(nèi)容如下所示,就是獲取到這些傳遞過來的參數(shù)信息,然后交給基類處理消息的簽名內(nèi)容。
#region 具體處理邏輯 string echoString = HttpContext.Current.Request.QueryString["echoStr"]; string signature = HttpContext.Current.Request.QueryString["msg_signature"];//企業(yè)號的 msg_signature string timestamp = HttpContext.Current.Request.QueryString["timestamp"]; string nonce = HttpContext.Current.Request.QueryString["nonce"]; string decryptEchoString = ""; if (new CorpBasicApi().CheckSignature(token, signature, timestamp, nonce, corpId, encodingAESKey, echoString, ref decryptEchoString)) { if (!string.IsNullOrEmpty(decryptEchoString)) { HttpContext.Current.Response.Write(decryptEchoString); HttpContext.Current.Response.End(); } } #endregion
驗證代碼部門如下所示。
/// <summary> /// 驗證企業(yè)號簽名 /// </summary> /// <param name="token">企業(yè)號配置的Token</param> /// <param name="signature">簽名內(nèi)容</param> /// <param name="timestamp">時間戳</param> /// <param name="nonce">nonce參數(shù)</param> /// <param name="corpId">企業(yè)號ID標識</param> /// <param name="encodingAESKey">加密鍵</param> /// <param name="echostr">內(nèi)容字符串</param> /// <param name="retEchostr">返回的字符串</param> /// <returns></returns> 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; }
上面介紹了,微信企業(yè)號對URL的驗證過程,還有另外一個消息處理過程,就是微信服務器把消息發(fā)送給我們自己的應用服務器進行處理的過程,我們應用服務器需要在收到消息后,及時進行常規(guī)回復處理。
也就是下面的代碼邏輯。
if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } if (!string.IsNullOrEmpty(postString)) { Execute(postString, accountInfo); } }
同樣,我們給微信服務器回應消息的時候,我們也需要獲得相應的參數(shù),然后再行構造信息回答。
string echoString = HttpContext.Current.Request.QueryString["echoStr"]; string signature = HttpContext.Current.Request.QueryString["msg_signature"];//企業(yè)號的 msg_signature string timestamp = HttpContext.Current.Request.QueryString["timestamp"]; string nonce = HttpContext.Current.Request.QueryString["nonce"];
而另外一些參數(shù)信息,則是來源于我們企業(yè)號賬號的配置參數(shù)。
//獲取配置參數(shù)并對加解密函數(shù)初始化 string CorpToken = accountInfo.Token; string AESKey = accountInfo.EncodingAESKey; string CorpId = accountInfo.CorpID;
然后使用微信提供的消息加解密類,就可以順利對消息進行加解密的處理了。具體操作代碼如下所示。
//根據(jù)參數(shù)信息,初始化微信對應的消息加密解密類 WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(CorpToken, AESKey, CorpId); //對收到的密文進行解析處理 string sMsg = ""; // 解析之后的明文 int flag = wxcpt.DecryptMsg(signature, timestamp, nonce, postStr, ref sMsg); if (flag == 0) { //LogTextHelper.Info("記錄解密后的數(shù)據(jù):"); //LogTextHelper.Info(sMsg);//記錄解密后的數(shù)據(jù) CorpApiDispatch dispatch = new CorpApiDispatch(); string responseContent = dispatch.Execute(sMsg); //加密后并發(fā)送 //LogTextHelper.Info(responseContent); string encryptResponse = ""; timestamp = DateTime.Now.DateTimeToInt().ToString(); wxcpt.EncryptMsg(responseContent, timestamp, nonce, ref encryptResponse, ref signature); HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; HttpContext.Current.Response.Write(encryptResponse); } else { LogTextHelper.Info("解密消息失??!"); }
最終,我們把解密完成的消息交給對應的封裝類進行統(tǒng)一處理就可以了。
CorpApiDispatch dispatch = new CorpApiDispatch(); string responseContent = dispatch.Execute(sMsg);
這樣我們在企業(yè)號API的封裝,就可以只需要關注消息如何應答的邏輯就可以了,其他的不用關心。
感謝各位的閱讀!關于“C#開發(fā)微信之微信企業(yè)號消息和事件接收處理及解密的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。