asp.net如何開發(fā)微信驗證消息功能

這篇文章主要介紹了asp.net如何開發(fā)微信驗證消息功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

專注于為中小企業(yè)提供網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)遂溪免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

驗證消息的真實性

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

public override void OnActionExecuting(ActionExecutingContext filterContext)方法

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

asp.net如何開發(fā)微信驗證消息功能

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

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

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

Host: qy.weixin.qq.com

方法重寫,實現(xiàn)對消息的驗證

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

asp.net如何開發(fā)微信驗證消息功能

asp.net如何開發(fā)微信驗證消息功能

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

asp.net如何開發(fā)微信驗證消息功能

代碼示例

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"] };
      //驗證
      if (CheckSignature(model))
      {
        base.OnActionExecuting(filterContext);
      }      
    }

    private bool CheckSignature(Model.FormatModel.WeChatMsgRequestModel model)
    {
      string signature, timestamp, nonce, tempStr;
      //獲取請求來的參數(shù)
      signature = model.msg_signature;
      timestamp = model.timestamp;
      nonce = model.nonce;
      //創(chuàng)建數(shù)組,將 Token, timestamp, nonce 三個參數(shù)加入數(shù)組
      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")
          {
            //從請求的數(shù)據(jù)流中獲取請求信息
            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);
        }
      }
    }

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“asp.net如何開發(fā)微信驗證消息功能”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

分享標(biāo)題:asp.net如何開發(fā)微信驗證消息功能
文章鏈接:http://muchs.cn/article48/jpehhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版面包屑導(dǎo)航、服務(wù)器托管域名注冊、網(wǎng)站導(dǎo)航、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)