您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何使用ASP.NET進(jìn)行微信開(kāi)發(fā)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何使用ASP.NET進(jìn)行微信開(kāi)發(fā)”吧!
公眾平臺(tái)用戶(hù)提交信息后,微信服務(wù)器將發(fā)送GET請(qǐng)求到填寫(xiě)的URL上,并且?guī)纤膫€(gè)參數(shù):
開(kāi)發(fā)者通過(guò)檢驗(yàn)signature對(duì)請(qǐng)求進(jìn)行校驗(yàn)(下面有校驗(yàn)方式)。若確認(rèn)此次GET請(qǐng)求來(lái)自微信服務(wù)器,請(qǐng)?jiān)瓨臃祷豦chostr參數(shù)內(nèi)容,則接入生效,否則接入失敗。
signature結(jié)合了開(kāi)發(fā)者填寫(xiě)的token參數(shù)和請(qǐng)求中的timestamp參數(shù)、nonce參數(shù)。
加密/校驗(yàn)流程:
1. 將token、timestamp、nonce三個(gè)參數(shù)進(jìn)行字典序排序
2. 將三個(gè)參數(shù)字符串拼接成一個(gè)字符串進(jìn)行sha1加密
3. 開(kāi)發(fā)者獲得加密后的字符串可與signature對(duì)比,標(biāo)識(shí)該請(qǐng)求來(lái)源于微信
/// <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對(duì)比,標(biāo)識(shí)該請(qǐng)求來(lái)源于微信 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)普通微信用戶(hù)向公眾賬號(hào)發(fā)消息時(shí),微信服務(wù)器將POST該消息到填寫(xiě)的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ā)來(lái)的請(qǐng)求 /// </summary> /// <param name="xml"></param> public void processRequest(String xml) { try { // xml請(qǐng)求解析 Hashtable requestHT = WeixinServer.ParseXml(xml); // 發(fā)送方帳號(hào)(open_id) string fromUserName = (string)requestHT["FromUserName"]; // 公眾帳號(hào) string toUserName = (string)requestHT["ToUserName"]; // 消息類(lèi)型 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) { // 事件類(lèi)型 String eventType = (string)requestHT["Event"]; // 訂閱 if (eventType==ReqEventType.Subscribe) { Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關(guān)注!,")); } // 取消訂閱 else if (eventType==ReqEventType.Unsubscribe) { // TODO 取消訂閱后用戶(hù)再收不到公眾號(hào)發(fā)送的消息,因此不需要回復(fù)消息 } // 自定義菜單點(diǎn)擊事件 else if (eventType==ReqEventType.CLICK) { // TODO 自定義菜單權(quán)沒(méi)有開(kāi)放,暫不處理該類(lèi)消息 } } 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ā)來(lái)的請(qǐng)求 /// </summary> /// <param name="xml"></param> public void processRequest(String xml) { try { // xml請(qǐng)求解析 Hashtable requestHT = WeixinServer.ParseXml(xml); // 發(fā)送方帳號(hào)(open_id) string fromUserName = (string)requestHT["FromUserName"]; // 公眾帳號(hào) string toUserName = (string)requestHT["ToUserName"]; // 消息類(lèi)型 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) { // 事件類(lèi)型 String eventType = (string)requestHT["Event"]; // 訂閱 if (eventType==ReqEventType.Subscribe) { Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關(guān)注!,")); } // 取消訂閱 else if (eventType==ReqEventType.Unsubscribe) { // TODO 取消訂閱后用戶(hù)再收不到公眾號(hào)發(fā)送的消息,因此不需要回復(fù)消息 } // 自定義菜單點(diǎn)擊事件 else if (eventType==ReqEventType.CLICK) { // TODO 自定義菜單權(quán)沒(méi)有開(kāi)放,暫不處理該類(lèi)消息 } } else if (msgType == ReqMsgType.Location) { } } catch (Exception e) { } }</pre><br> <pre></pre> <br> <br>
感謝各位的閱讀,以上就是“如何使用ASP.NET進(jìn)行微信開(kāi)發(fā)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何使用ASP.NET進(jìn)行微信開(kāi)發(fā)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。