想要微信開發,首先要有個服務器,但是自己沒有。這時候可以用花生殼,將內網映射到公網上,這樣就可以在公網訪問自己的網站了。
然后要寫一個接入代碼,而微信上只有php是示例。這里附上asp.net的示例。
首先創建一個Default.aspx。在Page_Load里進行檢驗:(MyLog是日志類,可以忽略) 關于checkSignature()就和所查到的差不多了。這里貼一下
1
2
3
4
5
6
7
8
9
10
11
12
|
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()就和所查到的差不多了。這里貼一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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次即可,別像我這樣只點一次啊!!!