一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - asp.net開發微信公眾平臺之驗證消息的真實性

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

2019-12-16 13:14hebedich ASP.NET教程

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

驗證消息的真實性

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

public override void OnActionExecuting(ActionExecutingContext filterContext)方法

新建數據模型

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

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

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

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

Host: qy.weixin.qq.com

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

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

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

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

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

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

代碼示例

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91久久精品视频 | 国产日日操 | 成年人在线免费观看视频网站 | 日本一区二区视频在线 | 我要看靠逼片 | 色琪琪久久草在线视频 | 午夜深情在线观看免费 | 国产午夜免费不卡精品理论片 | 丰满大乳欲妇三级k8 | 2015小明台湾永久区域免费 | 免费看隐私美女 | 日本偷偷操 | daring国家队在线观看樱花动漫 | 国产精品久久99 | 欧美一卡2卡3卡无卡 | 91制片在线观看 | 日韩每日更新 | 蜜桃视频在线观看www | 天天射寡妇射 | 人人人人人看碰人人免费 | 男人的天堂久久 | 国产123区在线视频观看 | 国产趴着打光屁股sp抽打 | 免费xxxx日本大片在线观看 | 精品一区二区视频 | 成年人免费在线视频 | 天天色天天综合网 | 毛片在线网址 | 小sao货ji巴cao死你视频 | 日本免费高清在线 | 久久久久激情免费观看 | 亚洲第一在线播放 | piss美女厕所小便 | 亚洲福利在线观看 | 久久精品中文字幕 | 免费精品99久久国产综合精品 | 免费观看的毛片 | 日韩a一级欧美一级 | 日本中年japanesebear | 日本伊人色综合网 | 欧美日韩看看2015永久免费 |