ASP.NET微信開發(fā)(接口指南)
公眾平臺用戶提交信息后,微信服務(wù)器將發(fā)送GET請求到填寫的URL上,并且?guī)纤膫€(gè)參數(shù):
開發(fā)者通過檢驗(yàn)signature對請求進(jìn)行校驗(yàn)(下面有校驗(yàn)方式)。若確認(rèn)此次GET請求來自微信服務(wù)器,請?jiān)瓨臃祷豦chostr參數(shù)內(nèi)容,則接入生效,否則接入失敗。
signature結(jié)合了開發(fā)者填寫的token參數(shù)和請求中的timestamp參數(shù)、nonce參數(shù)。
加密/校驗(yàn)流程:
- 1. 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序
- 2. 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密
- 3. 開發(fā)者獲得加密后的字符串可與signature對比,標(biāo)識該請求來源于微信
/// <summary> /// 驗(yàn)證簽名 /// </summary> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <returns></returns> public static bool CheckSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序 Array.Sort<String>(arr); StringBuilder content = new StringBuilder(); for (int i = 0; i < arr.Length; i++) { content.Append(arr[i]); } String tmpStr = SHA1_Encrypt(content.ToString()); // 將sha1加密后的字符串可與signature對比,標(biāo)識該請求來源于微信 return tmpStr != null ? tmpStr.Equals(signature) : false; } /// <summary> /// 使用缺省密鑰給字符串加密 /// </summary> /// <param name="Source_String"></param> /// <returns></returns> public static string SHA1_Encrypt(string Source_String) { byte[] StrRes = Encoding.Default.GetBytes(Source_String); HashAlgorithm iSHA = new SHA1CryptoServiceProvider(); StrRes = iSHA.ComputeHash(StrRes); StringBuilder EnText = new StringBuilder(); foreach (byte iByte in StrRes) { EnText.AppendFormat("{0:x2}", iByte); } return EnText.ToString(); }
接入后是消息推送當(dāng)普通微信用戶向公眾賬號發(fā)消息時(shí),微信服務(wù)器將POST該消息到填寫的URL上。
protected void Page_Load(object sender, EventArgs e) { if (Request.HttpMethod.ToUpper() == "GET") { // 微信加密簽名 string signature = Request.QueryString["signature"]; // 時(shí)間戳 string timestamp = Request.QueryString["timestamp"]; // 隨機(jī)數(shù) string nonce = Request.QueryString["nonce"]; // 隨機(jī)字符串 string echostr = Request.QueryString["echostr"]; if (WeixinServer.CheckSignature(signature, timestamp, nonce)) { Response.Write(echostr); } } else if (Request.HttpMethod.ToUpper() == "POST") { StreamReader stream = new StreamReader(Request.InputStream); string xml = stream.ReadToEnd(); processRequest(xml); } } /// <summary> /// 處理微信發(fā)來的請求 /// </summary> /// <param name="xml"></param> public void processRequest(String xml) { try { // xml請求解析 Hashtable requestHT = WeixinServer.ParseXml(xml); // 發(fā)送方帳號(open_id) string fromUserName = (string)requestHT["FromUserName"]; // 公眾帳號 string toUserName = (string)requestHT["ToUserName"]; // 消息類型 string msgType = (string)requestHT["MsgType"]; //文字消息 if (msgType == ReqMsgType.Text) { // Response.Write(str); string content = (string)requestHT["Content"]; if(content=="1") { // Response.Write(str); Response.Write(GetNewsMessage(toUserName, fromUserName)); return; } if (content == "2") { Response.Write(GetUserBlogMessage(toUserName, fromUserName)); return; } if (content == "3") { Response.Write(GetGroupMessage(toUserName, fromUserName)); return; } if (content == "4") { Response.Write(GetWinePartyMessage(toUserName, fromUserName)); return; } Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); } else if (msgType == ReqMsgType.Event) { // 事件類型 String eventType = (string)requestHT["Event"]; // 訂閱 if (eventType==ReqEventType.Subscribe) { Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關(guān)注!,")); } // 取消訂閱 else if (eventType==ReqEventType.Unsubscribe) { // TODO 取消訂閱后用戶再收不到公眾號發(fā)送的消息,因此不需要回復(fù)消息 } // 自定義菜單點(diǎn)擊事件 else if (eventType==ReqEventType.CLICK) { // TODO 自定義菜單權(quán)沒有開放,暫不處理該類消息 } } else if (msgType == ReqMsgType.Location) { } } catch (Exception e) { } }<pre name="code" class="csharp"> protected void Page_Load(object sender, EventArgs e) { if (Request.HttpMethod.ToUpper() == "GET") { // 微信加密簽名 string signature = Request.QueryString["signature"]; // 時(shí)間戳 string timestamp = Request.QueryString["timestamp"]; // 隨機(jī)數(shù) string nonce = Request.QueryString["nonce"]; // 隨機(jī)字符串 string echostr = Request.QueryString["echostr"]; if (WeixinServer.CheckSignature(signature, timestamp, nonce)) { Response.Write(echostr); } } else if (Request.HttpMethod.ToUpper() == "POST") { StreamReader stream = new StreamReader(Request.InputStream); string xml = stream.ReadToEnd(); processRequest(xml); } } /// <summary> /// 處理微信發(fā)來的請求 /// </summary> /// <param name="xml"></param> public void processRequest(String xml) { try { // xml請求解析 Hashtable requestHT = WeixinServer.ParseXml(xml); // 發(fā)送方帳號(open_id) string fromUserName = (string)requestHT["FromUserName"]; // 公眾帳號 string toUserName = (string)requestHT["ToUserName"]; // 消息類型 string msgType = (string)requestHT["MsgType"]; //文字消息 if (msgType == ReqMsgType.Text) { // Response.Write(str); string content = (string)requestHT["Content"]; if(content=="1") { // Response.Write(str); Response.Write(GetNewsMessage(toUserName, fromUserName)); return; } if (content == "2") { Response.Write(GetUserBlogMessage(toUserName, fromUserName)); return; } if (content == "3") { Response.Write(GetGroupMessage(toUserName, fromUserName)); return; } if (content == "4") { Response.Write(GetWinePartyMessage(toUserName, fromUserName)); return; } Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); } else if (msgType == ReqMsgType.Event) { // 事件類型 String eventType = (string)requestHT["Event"]; // 訂閱 if (eventType==ReqEventType.Subscribe) { Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關(guān)注!,")); } // 取消訂閱 else if (eventType==ReqEventType.Unsubscribe) { // TODO 取消訂閱后用戶再收不到公眾號發(fā)送的消息,因此不需要回復(fù)消息 } // 自定義菜單點(diǎn)擊事件 else if (eventType==ReqEventType.CLICK) { // TODO 自定義菜單權(quán)沒有開放,暫不處理該類消息 } } else if (msgType == ReqMsgType.Location) { } } catch (Exception e) { } }</pre><br> <pre></pre> <br> <br>
本文已被整理到了《ASP.NET微信開發(fā)教程匯總》,歡迎大家學(xué)習(xí)閱讀。
以上就是關(guān)于ASP.NET微信開發(fā)接口指南的相關(guān)內(nèi)容介紹,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
利用Typings為Visual Studio Code實(shí)現(xiàn)智能提示功能
最近在學(xué)習(xí)Node.js及ThinkJS這個(gè)框架,用vscode作為開發(fā)環(huán)境。默認(rèn)情況下vscode對ThinkJS的代碼提示并不好,所以研究了一下,原來可以同通過Typings來讓vscode擁有強(qiáng)大的智能代碼提示功能。下面本文就介紹了如何利用Typings為Visual Studio Code實(shí)現(xiàn)智能提示功能。2017-02-02使用Hangfire+.NET?6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)
這篇文章主要介紹了使用Hangfire+.NET?6實(shí)現(xiàn)定時(shí)任務(wù)管理,通過引入Hangfire相關(guān)的Nuget包并對Hangfire進(jìn)行服務(wù)配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實(shí)例
最近項(xiàng)目中有使用到SQLite數(shù)據(jù)庫,于是查找資料,編寫了一個(gè)ASP.NET基于C#語言的SQLite數(shù)據(jù)庫操作實(shí)例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫類似.2009-12-12asp.net遍歷文件夾下所有子文件夾并綁定到gridview上的方法
這篇文章主要介紹了asp.net遍歷文件夾下所有子文件夾并且遍歷配置文件某一節(jié)點(diǎn)中所有key,value并且綁定到GridView上,需要的朋友可以參考下2014-08-08asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中
asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中的實(shí)現(xiàn)代碼。2009-05-05Asp.Net使用服務(wù)器控件Image/ImageButton顯示本地圖片的方法
Image/ImageButton服務(wù)器控件顯示本地的圖片,實(shí)現(xiàn)思路是數(shù)據(jù)庫中存放了圖片的相對地址,讀取數(shù)據(jù)庫中的地址,用控件加載顯示圖片。具體實(shí)現(xiàn)步驟大家參考下本文2017-08-08