微信開發(一) asp.net接入_實用技巧

來源:互聯網
上載者:User

 

        想要微信開發,首先要有個伺服器,但是自己沒有。這時候可以用花生殼,將內網映射到公網上,這樣就可以在公網訪問自己的網站了。具體見:http://www.jb51.net/article/83783.htm

        然後要寫一個接入代碼,而微信上只有php是樣本。這裡附上asp.net的樣本。

        首先建立一個Default.aspx。在Page_Load裡進行檢驗:(MyLog是日誌類,可以忽略)   關於checkSignature()就和所查到的差不多了。這裡貼一下
     

 MyLog.DebugInfo("request default.aspx"); String echoStr = Request.QueryString["echostr"]; MyLog.DebugInfo("echoStr:"+echoStr); if (this.checkSignature()) { if(!string.IsNullOrEmpty(echoStr)){ MyLog.DebugInfo("echostr:" + echoStr); Response.Write(echoStr); Response.End(); }  }

最最主要的是那句Response.End(),不加這一句怎麼樣都接不進去(希望有大神告知)。 關於checkSignature()就和所查到的差不多了。這裡貼一下

private bool checkSignature(){  string signature = Request["signature"]; string timestamp = Request["timestamp"]; string nonce = Request["nonce"]; MyLog.DebugInfo(String.Format("signature:{0},timestamp:{1},nonce:{2}", signature, timestamp, nonce)); string token = TOKEN; string[] tmpArr = new string[] { token, timestamp, nonce }; Array.Sort(tmpArr); string tmpStr = string.Join("", tmpArr); //sha1加密 System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider(); byte[] secArr = sha1.ComputeHash(System.Text.Encoding.Default.GetBytes(tmpStr)); tmpStr = BitConverter.ToString(secArr).Replace("-", "").ToLower(); MyLog.DebugInfo(String.Format("after parse:{0}", tmpStr)); if (tmpStr == signature) { MyLog.DebugInfo("true"); return true; } else { return false; }}

        這裡主要是因為那個Response.End()的問題,導致我搞了許久,特此記錄一下,希望協助能協助到的人。

        還有一點可能是因為微信伺服器的原因Token驗證失敗,多點2次即可,別像我這樣只點一次啊!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.