c#微信公衆號開發一----基本設置,服務器配置token驗證

c#微信公衆號開發----基本設置

參考微信官方文檔html

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.htmlc#

開發→基本配置服務器

公衆號開發信息

 

 注:1.記錄好開發者密碼,會在程序中驗證過程當中使用到。微信

2.經過appid和appsecret調用access_token時,至有在ip白名單的ip才能成功調用。app

 

 

服務器配置

 

若此處開啓服務器配置,設置的自動回覆和自定義菜單將所有失效。必須在程序中重寫相關方法。post

 

 

點擊修改配置,token爲隨意填寫的參數加密

 

 

我是用的是通常處理程序編寫的微信接口token驗證,參數參考官方文檔。代碼以下:url

 

開發者經過檢驗signature對請求進行校驗。若確認這次GET請求來自微信服務器,請原樣返回echostr參數內容,則接入生效,成爲開發者成功,不然接入失敗。加密/校驗流程以下:spa

1)將token、timestamp、nonce三個參數進行字典序排序code

2)將三個參數字符串拼接成一個字符串進行sha1加密

3)開發者得到加密後的字符串可與signature對比,標識該請求來源於微信。

 

 1 public void ProcessRequest(HttpContext context){
 2     //驗證token
 3     string postString = string.Empty;
 4     string token ="aabbcc";   //驗證token,隨意填寫  
 5     if(string.IsNullEmpty(token)){
 6         return ;
 7     }
 8     string echoString = HttpContext.Current.Request.QueryString["echoStr"];
 9     string signature = HttpContext.Current.Request.QueryString["sianature"];
10     string timestamp = HttpContext.Current.Request.QueryString["timestamp"];
11     string nonce = HttpContext.Current.Request.QueryString["nonce"];
12     if(CheckSignature(token,signature,timestamp,nonce)){
13         if(!string.IsNullOrEmpty(echiString)){
14           HttpContext.Current.Response.Write(echoString);
15           HttpContext.Current.Response.End();
16        } 
17     }
18 }

 

 1          /// <summary>
 2         /// 驗證微信簽名
 3         /// </summary>
 4         /// <param name="token">token</param>
 5         /// <param name="signature">簽名</param>
 6         /// <param name="timestamp">時間戳</param>
 7         /// <param name="nonce">隨機數</param>
 8         /// <returns></returns>
 9         public static bool CheckSignature(string token,
10  string signature, string timestamp, string nonce)
11         {
12             string[] ArrTmp = { token, timestamp, nonce };
13             //字典排序
14             Array.Sort(ArrTmp);
15             //拼接
16             string tmpStr = string.Join("", ArrTmp);
17             //sha1驗證
18             tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
19             //tmpStr = Membership.CreateUser(tmpStr, "SHA1");
20             tmpStr = tmpStr.ToLower();
21 
22             if (tmpStr == signature)
23             {
24                 return true;
25             }
26             else
27             {
28                 return false;
29             }
30         }

 

 將編寫的代碼路徑,填寫到url裏,前面填寫的「aabbcc」,此時token裏填寫的也必須爲「aabbcc」。

token必須保持一致,若不一致會彈出提示。

 

相關文章
相關標籤/搜索