.net 微信Token驗證

首次接受這個項目,看了微信的API,雲裏霧裏,通過幾經測試,理清思路服務器

開發者自個申請,微信API給出四個參數:微信

下面我解釋下測試

signature 是微信加密簽名 即:微信服務器將 timetamp nonce  token(你提交的)使用SHA1加密後 會使用GET方式發送給你。網站

timestamp是騰訊服務器發送的一個時間戳加密

nonce是騰訊服務器發送的一個隨機數spa

成功申請的關鍵是在下面,上面的幾個參數只是 微信提供 用戶請求--->你本身的網站 的一個驗證方式。orm

echostr就是 騰訊服務器發送的一個隨機字符串 這個須要你在本身網站接受後 使用輸出流 Respons.Write(echostr);排序

記住最好在輸出完畢終止流 Response.End(); 而後就能夠成功申請了。token

參數 描述
signature 微信加密簽名
timestamp 時間戳
nonce 隨機數
echostr 隨機字符串 
  1.  private void Valid()  
  2.     {  
  3.         string echoStr = Request.QueryString["echoStr"].ToString();  
  4.         if (CheckSignature())  
  5.         {  
  6.             if (!string.IsNullOrEmpty(echoStr))  
  7.             {  
  8.                 Response.Write(echoStr);  
  9.                 Response.End();  
  10.             }  
  11.         }  
  12.     }  
  1.  private bool CheckSignature()  
  2.     {  
  3.         string signature = Request.QueryString["signature"].ToString();  
  4.         string timestamp = Request.QueryString["timestamp"].ToString();  
  5.         string nonce = Request.QueryString["nonce"].ToString();  
  6.         string[] ArrTmp = { Token, timestamp, nonce };  
  7.         Array.Sort(ArrTmp);     //字典排序  
  8.         string tmpStr = string.Join("", ArrTmp);  
  9.         tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");  
  10.         tmpStr = tmpStr.ToLower();  
  11.         if (tmpStr == signature)  
  12.         {  
  13.             return true;  
  14.         }  
  15.         else 
  16.         {  
  17.             return false;  
  18.         }  
  19.     }  
相關文章
相關標籤/搜索