1、企業(yè)號回調(diào)模式的設(shè)置
和公眾號一樣,微信企業(yè)號如果需要進(jìn)行二次開發(fā),也是需要在后臺設(shè)置好對應(yīng)的回調(diào)參數(shù),如下界面所示。
設(shè)置好這些后,檢查通過后,我們就可以在自己微信應(yīng)用服務(wù)器上進(jìn)行消息的收發(fā)操作了。
在回調(diào)的消息入口處,我們需要對POST數(shù)據(jù)和普通的GET數(shù)據(jù)進(jìn)行分開處理,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) {
上面我們定義了一個一般應(yīng)用處理程序來對消息進(jìn)行處理。
然后我們分開不同的消息類型(POST、GET 方式),針對性的進(jìn)行處理。
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); }
2、微信回調(diào)消息的驗證
下面是微信對于回調(diào)模式,驗證URL的說明。
驗證URL有效性
當(dāng)你提交以上信息時,企業(yè)號將發(fā)送GET請求到填寫的URL上,GET請求攜帶四個參數(shù),企業(yè)在獲取時需要做urldecode處理,否則會驗證不成功。
參數(shù) | 描述 | 是否必帶 |
---|---|---|
msg_signature | 微信加密簽名,msg_signature結(jié)合了企業(yè)填寫的token、請求中的timestamp、nonce參數(shù)、加密的消息體 | 是 |
timestamp | 時間戳 | 是 |
nonce | 隨機數(shù) | 是 |
echostr | 加密的隨機字符串,以msg_encrypt格式提供。需要解密并返回echostr明文,解密后有random、msg_len、msg、$CorpID四個字段,其中msg即為echostr明文 | 首次校驗時必帶 |
企業(yè)通過參數(shù)msg_signature對請求進(jìn)行校驗,如果確認(rèn)此次GET請求來自企業(yè)號,那么企業(yè)應(yīng)用對echostr參數(shù)解密并原樣返回echostr明文(不能加引號),則接入驗證生效,回調(diào)模式才能開啟。
后續(xù)回調(diào)企業(yè)時都會在請求URL中帶上以上參數(shù)(echostr除外),校驗方式與首次驗證URL一致。
根據(jù)上面的說明,我們需要獲取這些參數(shù),然后調(diào)用微信提供的消息處理函數(shù)進(jìn)行加解密處理。
在驗證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標(biāo)識</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; }
3、企業(yè)號的消息處理
上面介紹了,微信企業(yè)號對URL的驗證過程,還有另外一個消息處理過程,就是微信服務(wù)器把消息發(fā)送給我們自己的應(yīng)用服務(wù)器進(jìn)行處理的過程,我們應(yīng)用服務(wù)器需要在收到消息后,及時進(jìn)行常規(guī)回復(fù)處理。
也就是下面的代碼邏輯。
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); } }
同樣,我們給微信服務(wù)器回應(yīng)消息的時候,我們也需要獲得相應(yīng)的參數(shù),然后再行構(gòu)造信息回答。
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ìn)行加解密的處理了。具體操作代碼如下所示。
//根據(jù)參數(shù)信息,初始化微信對應(yīng)的消息加密解密類 WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(CorpToken, AESKey, CorpId); //對收到的密文進(jìn)行解析處理 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("解密消息失?。?quot;); }
最終,我們把解密完成的消息交給對應(yīng)的封裝類進(jìn)行統(tǒng)一處理就可以了。
CorpApiDispatch dispatch = new CorpApiDispatch(); string responseContent = dispatch.Execute(sMsg);
這樣我們在企業(yè)號API的封裝,就可以只需要關(guān)注消息如何應(yīng)答的邏輯就可以了,其他的不用關(guān)心。
?
?更多C#開發(fā)微信門戶及應(yīng)用微信企業(yè)號的消息和事件的接收處理及解密相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Alat AI Hot

Undress AI Tool
Gambar buka pakaian secara percuma

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)