asp.net開發微信公眾平台之驗證訊息的真實性

來源:互聯網
上載者:User

   這篇文章主要介紹了asp.net開發微信公眾平台之驗證訊息的真實性的相關資料,需要的朋友可以參考下

  驗證訊息的真實性

  在MVC Controller所在項目中添加過濾器,在過濾器中重寫

  public override void OnActionExecuting(ActionExecutingContext filterContext)方法

  建立資料模型

  註:伺服器接收訊息時,不再是signature而是msg_signature

  微信伺服器推送訊息到伺服器的HTTP請求報文樣本

  POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6×tamp=1409659813&nonce=1372623149 HTTP/1.1

  Host: qy.weixin.qq.com

  方法重寫,實現對訊息的驗證

  調用微信接入時驗證的方法,不過參數需要小改動一下,採用建立的資料模型

  在Action方法或在Controller上添加過濾器屬性

  程式碼範例

  Model

  ?

1 2 3 4 5 6 7 8 9 10 /// <summary> /// 微信推送訊息模型 /// </summary> public class WeChatMsgRequestModel { public string timestamp { get; set; } public string nonce { get; set; }   public string msg_signature { get; set; } }

  Filter

  ?

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class WeChatRequestValidAttribute : ActionFilterAttribute { private const string Token = "StupidMe";   public override void OnActionExecuting(ActionExecutingContext filterContext) { //參數適配 Model.FormatModel.WeChatMsgRequestModel model = new Model.FormatModel.WeChatMsgRequestModel() { nonce= filterContext.HttpContext.Request.QueryString["nonce"],msg_signature= filterContext.HttpContext.Request.QueryString["msg_signature"],timestamp= filterContext.HttpContext.Request.QueryString["timestamp"] }; //驗證 if (CheckSignature(model)) { base.OnActionExecuting(filterContext); } }   private bool CheckSignature(Model.FormatModel.WeChatMsgRequestModel model) { string signature, timestamp, nonce, tempStr; //擷取請求來的參數 signature = model.msg_signature; timestamp = model.timestamp; nonce = model.nonce; //建立數組,將 Token, timestamp, nonce 三個參數加入數組 string[] array = { Token, timestamp, nonce }; //進行排序 Array.Sort(array); //拼接為一個字串 tempStr = String.Join("", array); //對字串進行 SHA1加密 tempStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tempStr, "SHA1").ToLower(); //判斷signature 是否正確 if (tempStr.Equals(signature)) { return true; } else { return false; } } }

  Controller Code

  ?

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 27 28 29 30 31 /// <summary> /// 日誌助手 /// </summary> private static Common.LogHelper logger = new Common.LogHelper(typeof(HomeController));   [Filters.WeChatRequestValid] public void Valid(Model.FormatModel.WeChatMsgRequestModel model) { if (ModelState.IsValid) { try { //判斷是否是POST請求 if (HttpContext.Request.HttpMethod.ToUpper() == "POST") { //從請求的資料流中擷取請求資訊 using (Stream stream = HttpContext.Request.InputStream) { byte[] postBytes = new byte[stream.Length]; stream.Read(postBytes, 0, (int)stream.Length); string postString = System.Text.Encoding.UTF8.GetString(postBytes); Handle(postString,model); } } } catch (Exception ex) { logger.Error("發生異常,異常資訊:" + ex.Message + ex.StackTrace); } } }

  以上所述就是本文的全部內容 了,希望大家能夠喜歡。

聯繫我們

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