婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > asp.net開發微信公眾平臺之驗證消息的真實性

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

熱門標簽:河北網絡回撥外呼系統 400免費電話怎么辦理 威海電銷 寧夏機器人電銷 400電話辦理最優質 關于宗地圖標注技術規范 t3出行地圖標注怎么做 河南語音外呼系統公司 外呼電銷機器人軟件

驗證消息的真實性

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

public override void OnActionExecuting(ActionExecutingContext filterContext)方法

新建數據模型

注:服務器接收消息時,不再是signature而是msg_signature

微信服務器推送消息到服務器的HTTP請求報文示例

POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6timestamp=1409659813nonce=1372623149 HTTP/1.1

Host: qy.weixin.qq.com

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

調用微信接入時驗證的方法,不過參數需要小改動一下,采用新建的數據模型

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

代碼示例

Model

/// summary>
  /// 微信推送消息模型
  /// /summary>
  public class WeChatMsgRequestModel
  {
    public string timestamp { get; set; }
    public string nonce { get; set; }

    public string msg_signature { get; set; }
  }

Filter

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.timest
      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

/// 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);
        }
      }
    }      

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

您可能感興趣的文章:
  • ASP.NET微信開發(接口指南)
  • asp.net微信開發(永久素材管理)
  • asp.net微信開發(高級群發圖文)
  • asp.net微信開發(高級群發文本)
  • asp.net微信開發(用戶分組管理)
  • asp.net微信開發(已關注用戶管理)
  • asp.net微信開發(自定義會話管理)
  • asp.net微信開發(開發者接入)
  • asp.net開發微信公眾平臺之獲取用戶消息并處理
  • ASP.NET MVC5+EF6+EasyUI后臺管理系統 微信公眾平臺開發之資源環境準備

標簽:咸寧 吉林 淮北 池州 廣元 樂山 固原 賀州

巨人網絡通訊聲明:本文標題《asp.net開發微信公眾平臺之驗證消息的真實性》,本文關鍵詞  asp.net,開發,微信,公眾,平臺,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net開發微信公眾平臺之驗證消息的真實性》相關的同類信息!
  • 本頁收集關于asp.net開發微信公眾平臺之驗證消息的真實性的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 齐河县| 香港| 鸡西市| 西宁市| 平远县| 清丰县| 玉龙| 郸城县| 苏尼特右旗| 卢湾区| 靖边县| 疏附县| 当阳市| 峨眉山市| 宕昌县| 桦川县| 万州区| 五莲县| 交口县| 同仁县| 章丘市| 嘉黎县| 孟津县| 吴堡县| 陈巴尔虎旗| 赤水市| 叙永县| 赣榆县| 镇江市| 类乌齐县| 南召县| 扬州市| 尉犁县| 天全县| 屏山县| 乌海市| 迭部县| 隆化县| 昭平县| 大埔区| 邵武市|