溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用asp.net怎么對(duì)消息的真實(shí)性進(jìn)行驗(yàn)證

發(fā)布時(shí)間:2020-12-28 15:05:00 來(lái)源:億速云 閱讀:156 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

使用asp.net怎么對(duì)消息的真實(shí)性進(jìn)行驗(yàn)證?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

驗(yàn)證消息的真實(shí)性

在MVC Controller所在項(xiàng)目中添加過(guò)濾器,在過(guò)濾器中重寫(xiě)

public override void OnActionExecuting(ActionExecutingContext filterContext)方法

新建數(shù)據(jù)模型

使用asp.net怎么對(duì)消息的真實(shí)性進(jìn)行驗(yàn)證

注:服務(wù)器接收消息時(shí),不再是signature而是msg_signature

微信服務(wù)器推送消息到服務(wù)器的HTTP請(qǐng)求報(bào)文示例

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

Host: qy.weixin.qq.com

方法重寫(xiě),實(shí)現(xiàn)對(duì)消息的驗(yàn)證

調(diào)用微信接入時(shí)驗(yàn)證的方法,不過(guò)參數(shù)需要小改動(dòng)一下,采用新建的數(shù)據(jù)模型

使用asp.net怎么對(duì)消息的真實(shí)性進(jìn)行驗(yàn)證

使用asp.net怎么對(duì)消息的真實(shí)性進(jìn)行驗(yàn)證

在Action方法或在Controller上添加過(guò)濾器屬性

使用asp.net怎么對(duì)消息的真實(shí)性進(jìn)行驗(yàn)證

代碼示例

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)
    {
      //參數(shù)適配
      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"] };
      //驗(yàn)證
      if (CheckSignature(model))
      {
        base.OnActionExecuting(filterContext);
      }      
    }

    private bool CheckSignature(Model.FormatModel.WeChatMsgRequestModel model)
    {
      string signature, timestamp, nonce, tempStr;
      //獲取請(qǐng)求來(lái)的參數(shù)
      signature = model.msg_signature;
      timestamp = model.timestamp;
      nonce = model.nonce;
      //創(chuàng)建數(shù)組,將 Token, timestamp, nonce 三個(gè)參數(shù)加入數(shù)組
      string[] array = { Token, timestamp, nonce };
      //進(jìn)行排序
      Array.Sort(array);
      //拼接為一個(gè)字符串
      tempStr = String.Join("", array);
      //對(duì)字符串進(jìn)行 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請(qǐng)求
          if (HttpContext.Request.HttpMethod.ToUpper() == "POST")
          {
            //從請(qǐng)求的數(shù)據(jù)流中獲取請(qǐng)求信息
            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("發(fā)生異常,異常信息:" + ex.Message + ex.StackTrace);
        }
      }
    }

看完上述內(nèi)容,你們掌握使用asp.net怎么對(duì)消息的真實(shí)性進(jìn)行驗(yàn)證的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI