這篇文章主要介紹了微信公眾號服務器驗證Token步驟圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
服務器驗證Token驗證分為以下及步驟
一,在微信公眾號平臺上設置
1.1打開微信公眾號平臺
1.2打開”開發“中的<基本配置>
1.3點擊基本配置頁面里的修改配置
1.4輸入URL:
url填寫:http://外網IP:端口號/wx 。外網IP請到騰訊云購買成功處查詢, http的端口號固定使用80,不可填寫其他。
Token:自主設置,這個token與公眾平臺wiki中常提的access_token不是一回事。這個token只用于驗證開發者服務器。(注:Token可以隨便寫 寫完記住留著備用)
EncodingAESKey:點擊隨機生成
現在選擇提交肯定是驗證token失敗,因為還需要完成代碼邏輯。改動原先main.py文件,新增handle.py 也可以用wbe文件
二,編寫后臺程序
我選用的是web,ashx一般處理程序頁面
代碼源碼:
namespace WEF{ /// <summary> /// Token 的摘要說明 /// </summary> public class Token : IHttpHandler { public void ProcessRequest(HttpContext context) { ProcesyanzhengsRequest(context);//執行下面方法 } public bool IsReusable { get { return false; } } public void ProcesyanzhengsRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string token = " ";//輸入你上面自己編寫的Token if (string.IsNullOrEmpty(token)) { return; } //取到Token接收到的值 string echoString = HttpContext.Current.Request.QueryString["echoStr"]; string signature = HttpContext.Current.Request.QueryString["signature"]; string timestamp = HttpContext.Current.Request.QueryString["timestamp"]; string nonce = HttpContext.Current.Request.QueryString["nonce"]; if (CheckSignature(token, signature, timestamp, nonce)) //判斷驗證是否正確 { if (!string.IsNullOrEmpty(echoString)) 正確返回微信服務器 { HttpContext.Current.Response.Write(echoString); HttpContext.Current.Response.End(); } } } /// <summary> /// 驗證微信簽名 /// </summary> public static bool CheckSignature(string token, string signature, string timestamp, string nonce) { string[] ArrTmp = { token, timestamp, nonce }; //字典排序 Array.Sort(ArrTmp); //拼接 string tmpStr = string.Join("", ArrTmp); //sha1驗證 tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); //tmpStr = Membership.CreateUser(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) //如果計算后得到的數值與傳過來的數值相等 { return true; //返回正確 } else { return false; //不相等 返回錯誤 } } }}
新聞熱點
疑難解答