ASP.NET微信開發(介面指南)_實用技巧

來源:互聯網
上載者:User

公眾平台使用者提交資訊後,微信伺服器將發送GET請求到填寫的URL上,並且帶上四個參數:

開發人員通過檢驗signature對請求進行校正(下面有校正方式)。若確認此次GET請求來自微信伺服器,請原樣返回echostr參數內容,則接入生效,否則接入失敗。

signature結合了開發人員填寫的token參數和請求中的timestamp參數、nonce參數。

加密/校正流程:

  • 1. 將token、timestamp、nonce三個參數進行字典序排序
  • 2. 將三個參數字串拼接成一個字串進行sha1加密
  • 3. 開發人員獲得加密後的字串可與signature對比,標識該請求來源於微信
/// <summary>  /// 驗證簽名  /// </summary>  /// <param name="signature"></param>  /// <param name="timestamp"></param>  /// <param name="nonce"></param>  /// <returns></returns>  public static bool CheckSignature(String signature, String timestamp, String nonce)  {  String[] arr = new String[] { token, timestamp, nonce };  // 將token、timestamp、nonce三個參數進行字典序排序  Array.Sort<String>(arr);   StringBuilder content = new StringBuilder();  for (int i = 0; i < arr.Length; i++)  {   content.Append(arr[i]);  }   String tmpStr = SHA1_Encrypt(content.ToString());    // 將sha1加密後的字串可與signature對比,標識該請求來源於微信  return tmpStr != null ? tmpStr.Equals(signature) : false;  }    /// <summary>  /// 使用預設密鑰給字串加密  /// </summary>  /// <param name="Source_String"></param>  /// <returns></returns>  public static string SHA1_Encrypt(string Source_String)  {  byte[] StrRes = Encoding.Default.GetBytes(Source_String);  HashAlgorithm iSHA = new SHA1CryptoServiceProvider();  StrRes = iSHA.ComputeHash(StrRes);  StringBuilder EnText = new StringBuilder();  foreach (byte iByte in StrRes)  {   EnText.AppendFormat("{0:x2}", iByte);  }  return EnText.ToString();  } 

接入後是訊息推送當普通微信使用者向公眾帳號發訊息時,微信伺服器將POST該訊息到填寫的URL上。

 protected void Page_Load(object sender, EventArgs e)  {   if (Request.HttpMethod.ToUpper() == "GET")  {   // 微信加密簽名   string signature = Request.QueryString["signature"];   // 時間戳記   string timestamp = Request.QueryString["timestamp"];   // 隨機數   string nonce = Request.QueryString["nonce"];   // 隨機字串   string echostr = Request.QueryString["echostr"];   if (WeixinServer.CheckSignature(signature, timestamp, nonce))   {   Response.Write(echostr);   }   }  else if (Request.HttpMethod.ToUpper() == "POST")  {    StreamReader stream = new StreamReader(Request.InputStream);   string xml = stream.ReadToEnd();    processRequest(xml);  }    }    /// <summary>  /// 處理微信發來的請求  /// </summary>  /// <param name="xml"></param>  public void processRequest(String xml)  {  try  {    // xml請求解析   Hashtable requestHT = WeixinServer.ParseXml(xml);    // 發送方帳號(open_id)   string fromUserName = (string)requestHT["FromUserName"];   // 公眾帳號   string toUserName = (string)requestHT["ToUserName"];   // 訊息類型   string msgType = (string)requestHT["MsgType"];    //文字訊息   if (msgType == ReqMsgType.Text)   {   // Response.Write(str);    string content = (string)requestHT["Content"];   if(content=="1")   {    // Response.Write(str);    Response.Write(GetNewsMessage(toUserName, fromUserName));    return;   }   if (content == "2")   {    Response.Write(GetUserBlogMessage(toUserName, fromUserName));    return;   }   if (content == "3")   {    Response.Write(GetGroupMessage(toUserName, fromUserName));    return;   }   if (content == "4")   {    Response.Write(GetWinePartyMessage(toUserName, fromUserName));    return;   }   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,"));    }   else if (msgType == ReqMsgType.Event)   {   // 事件類型   String eventType = (string)requestHT["Event"];   // 訂閱   if (eventType==ReqEventType.Subscribe)   {       Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關注!,"));      }   // 取消訂閱   else if (eventType==ReqEventType.Unsubscribe)   {    // TODO 取消訂閱後使用者再收不到公眾號發送的訊息,因此不需要回複訊息   }   // 自訂菜單點擊事件   else if (eventType==ReqEventType.CLICK)   {    // TODO 自訂菜單權沒有開放,暫不處理該類訊息   }   }   else if (msgType == ReqMsgType.Location)   {   }    }  catch (Exception e)  {    }  }<pre name="code" class="csharp"> protected void Page_Load(object sender, EventArgs e)  {   if (Request.HttpMethod.ToUpper() == "GET")  {   // 微信加密簽名   string signature = Request.QueryString["signature"];   // 時間戳記   string timestamp = Request.QueryString["timestamp"];   // 隨機數   string nonce = Request.QueryString["nonce"];   // 隨機字串   string echostr = Request.QueryString["echostr"];   if (WeixinServer.CheckSignature(signature, timestamp, nonce))   {   Response.Write(echostr);   }   }  else if (Request.HttpMethod.ToUpper() == "POST")  {    StreamReader stream = new StreamReader(Request.InputStream);   string xml = stream.ReadToEnd();    processRequest(xml);  }    }    /// <summary>  /// 處理微信發來的請求  /// </summary>  /// <param name="xml"></param>  public void processRequest(String xml)  {  try  {    // xml請求解析   Hashtable requestHT = WeixinServer.ParseXml(xml);    // 發送方帳號(open_id)   string fromUserName = (string)requestHT["FromUserName"];   // 公眾帳號   string toUserName = (string)requestHT["ToUserName"];   // 訊息類型   string msgType = (string)requestHT["MsgType"];    //文字訊息   if (msgType == ReqMsgType.Text)   {   // Response.Write(str);    string content = (string)requestHT["Content"];   if(content=="1")   {    // Response.Write(str);    Response.Write(GetNewsMessage(toUserName, fromUserName));    return;   }   if (content == "2")   {    Response.Write(GetUserBlogMessage(toUserName, fromUserName));    return;   }   if (content == "3")   {    Response.Write(GetGroupMessage(toUserName, fromUserName));    return;   }   if (content == "4")   {    Response.Write(GetWinePartyMessage(toUserName, fromUserName));    return;   }   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,"));    }   else if (msgType == ReqMsgType.Event)   {   // 事件類型   String eventType = (string)requestHT["Event"];   // 訂閱   if (eventType==ReqEventType.Subscribe)   {       Response.Write(GetMainMenuMessage(toUserName, fromUserName, "謝謝您的關注!,"));      }   // 取消訂閱   else if (eventType==ReqEventType.Unsubscribe)   {    // TODO 取消訂閱後使用者再收不到公眾號發送的訊息,因此不需要回複訊息   }   // 自訂菜單點擊事件   else if (eventType==ReqEventType.CLICK)   {    // TODO 自訂菜單權沒有開放,暫不處理該類訊息   }   }   else if (msgType == ReqMsgType.Location)   {   }    }  catch (Exception e)  {    }  }</pre><br> <pre></pre> <br> <br> 

本文已被整理到了《ASP.NET微信開發教程匯總》,歡迎大家學習閱讀。

以上就是關於ASP.NET微信開發介面指南的相關內容介紹,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.