Home WeChat Applet WeChat Development ASP.NET WeChat Development Interface Guide Detailed Introduction

ASP.NET WeChat Development Interface Guide Detailed Introduction

Mar 10, 2017 pm 02:34 PM
asp.net WeChat development

This article introduces the ASP.NET WeChat Development Interface Guide in detail. The development of WeChat public platform is relatively simple. Interested friends can refer to it.

After the public platform user submits the information, the WeChat server will send The GET request goes to the filled-in URL and carries four parameters:

ASP.NET WeChat Development Interface Guide Detailed Introduction

The developer verifies the request by checking the signature (the verification method is below). If it is confirmed that this GET request comes from the WeChat server, please return the echostr parameter content as it is, then the access will take effect, otherwise the access will fail.

signature combines the token parameter filled in by the developer with the timestamp parameter and nonce parameter in the request.

Encryption/verification process:

  • 1. Sort the three parameters token, timestamp, and nonce in lexicographic order

  • 2. Splice the three parameter strings into one string for sha1 encryption

  • 3. The developer can compare the encrypted string with the signature. Identifies that the request comes from WeChat

/// <summary> 
 /// 验证签名 
 /// </summary> 
 /// <param name="signature"></param> 
 /// <param name="timestamp"></param> 
 /// <param name="nonce"></param> 
 /// <returns></returns> 
 public static bool CheckSignature(String signature, String timestamp, String nonce) 
 { 
 String[] arr = new String[] { token, timestamp, nonce }; 
 // 将token、timestamp、nonce三个参数进行字典序排序 
 Array.Sort<String>(arr); 
 
 StringBuilder content = new StringBuilder(); 
 for (int i = 0; i < arr.Length; i++) 
 { 
  content.Append(arr[i]); 
 } 
 
 String tmpStr = SHA1_Encrypt(content.ToString()); 
 
 
 // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信 
 return tmpStr != null ? tmpStr.Equals(signature) : false; 
 } 
 
 
 /// <summary> 
 /// 使用缺省密钥给字符串加密 
 /// </summary> 
 /// <param name="Source_String"></param> 
 /// <returns></returns> 
 public static string SHA1_Encrypt(string Source_String) 
 { 
 byte[] StrRes = Encoding.Default.GetBytes(Source_String); 
 HashAlgorithm iSHA = new SHA1CryptoServiceProvider(); 
 StrRes = iSHA.ComputeHash(StrRes); 
 StringBuilder EnText = new StringBuilder(); 
 foreach (byte iByte in StrRes) 
 { 
  EnText.AppendFormat("{0:x2}", iByte); 
 } 
 return EnText.ToString(); 
 }
Copy after login


## After accessing, the message will be pushed as an ordinary WeChat user When sending a message to a public account, the WeChat server will POST the message to the filled-in URL.


 protected void Page_Load(object sender, EventArgs e) 
 { 
 
 if (Request.HttpMethod.ToUpper() == "GET") 
 { 
  // 微信加密签名 
  string signature = Request.QueryString["signature"]; 
  // 时间戳 
  string timestamp = Request.QueryString["timestamp"]; 
  // 随机数 
  string nonce = Request.QueryString["nonce"]; 
  // 随机字符串 
  string echostr = Request.QueryString["echostr"]; 
  if (WeixinServer.CheckSignature(signature, timestamp, nonce)) 
  { 
  Response.Write(echostr); 
  } 
 
 } 
 else if (Request.HttpMethod.ToUpper() == "POST") 
 { 
 
  StreamReader stream = new StreamReader(Request.InputStream); 
  string xml = stream.ReadToEnd(); 
 
  processRequest(xml); 
 } 
 
 
 } 
 
 
 /// <summary> 
 /// 处理微信发来的请求 
 /// </summary> 
 /// <param name="xml"></param> 
 public void processRequest(String xml) 
 { 
 try 
 { 
 
  // xml请求解析 
  Hashtable requestHT = WeixinServer.ParseXml(xml); 
 
  // 发送方帐号(open_id) 
  string fromUserName = (string)requestHT["FromUserName"]; 
  // 公众帐号 
  string toUserName = (string)requestHT["ToUserName"]; 
  // 消息类型 
  string msgType = (string)requestHT["MsgType"]; 
 
  //文字消息 
  if (msgType == ReqMsgType.Text) 
  { 
  // Response.Write(str); 
 
  string content = (string)requestHT["Content"]; 
  if(content=="1") 
  { 
   // Response.Write(str); 
   Response.Write(GetNewsMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "2") 
  { 
   Response.Write(GetUserBlogMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "3") 
  { 
   Response.Write(GetGroupMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "4") 
  { 
   Response.Write(GetWinePartyMessage(toUserName, fromUserName)); 
   return; 
  } 
  Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); 
 
  } 
  else if (msgType == ReqMsgType.Event) 
  { 
  // 事件类型 
  String eventType = (string)requestHT["Event"]; 
  // 订阅 
  if (eventType==ReqEventType.Subscribe) 
  { 
   
   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,")); 
   
  } 
  // 取消订阅 
  else if (eventType==ReqEventType.Unsubscribe) 
  { 
   // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息 
  } 
  // 自定义菜单点击事件 
  else if (eventType==ReqEventType.CLICK) 
  { 
   // TODO 自定义菜单权没有开放,暂不处理该类消息 
  } 
  } 
  else if (msgType == ReqMsgType.Location) 
  { 
  } 
 
 
 } 
 catch (Exception e) 
 { 
  
 } 
 }<pre name="code" class="csharp"> protected void Page_Load(object sender, EventArgs e) 
 { 
 
 if (Request.HttpMethod.ToUpper() == "GET") 
 { 
  // 微信加密签名 
  string signature = Request.QueryString["signature"]; 
  // 时间戳 
  string timestamp = Request.QueryString["timestamp"]; 
  // 随机数 
  string nonce = Request.QueryString["nonce"]; 
  // 随机字符串 
  string echostr = Request.QueryString["echostr"]; 
  if (WeixinServer.CheckSignature(signature, timestamp, nonce)) 
  { 
  Response.Write(echostr); 
  } 
 
 } 
 else if (Request.HttpMethod.ToUpper() == "POST") 
 { 
 
  StreamReader stream = new StreamReader(Request.InputStream); 
  string xml = stream.ReadToEnd(); 
 
  processRequest(xml); 
 } 
 
 
 } 
 
 
 /// <summary> 
 /// 处理微信发来的请求 
 /// </summary> 
 /// <param name="xml"></param> 
 public void processRequest(String xml) 
 { 
 try 
 { 
 
  // xml请求解析 
  Hashtable requestHT = WeixinServer.ParseXml(xml); 
 
  // 发送方帐号(open_id) 
  string fromUserName = (string)requestHT["FromUserName"]; 
  // 公众帐号 
  string toUserName = (string)requestHT["ToUserName"]; 
  // 消息类型 
  string msgType = (string)requestHT["MsgType"]; 
 
  //文字消息 
  if (msgType == ReqMsgType.Text) 
  { 
  // Response.Write(str); 
 
  string content = (string)requestHT["Content"]; 
  if(content=="1") 
  { 
   // Response.Write(str); 
   Response.Write(GetNewsMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "2") 
  { 
   Response.Write(GetUserBlogMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "3") 
  { 
   Response.Write(GetGroupMessage(toUserName, fromUserName)); 
   return; 
  } 
  if (content == "4") 
  { 
   Response.Write(GetWinePartyMessage(toUserName, fromUserName)); 
   return; 
  } 
  Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,")); 
 
  } 
  else if (msgType == ReqMsgType.Event) 
  { 
  // 事件类型 
  String eventType = (string)requestHT["Event"]; 
  // 订阅 
  if (eventType==ReqEventType.Subscribe) 
  { 
   
   Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,")); 
   
  } 
  // 取消订阅 
  else if (eventType==ReqEventType.Unsubscribe) 
  { 
   // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息 
  } 
  // 自定义菜单点击事件 
  else if (eventType==ReqEventType.CLICK) 
  { 
   // TODO 自定义菜单权没有开放,暂不处理该类消息 
  } 
  } 
  else if (msgType == ReqMsgType.Location) 
  { 
  } 
 
 
 } 
 catch (Exception e) 
 { 
  
 } 
 }
Copy after login

Copy after login


The above is an introduction to the relevant content of the ASP.NET WeChat Development Interface Guide. I hope it will be helpful to everyone's study.

The above is the detailed content of ASP.NET WeChat Development Interface Guide Detailed Introduction. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP WeChat development: How to implement message encryption and decryption PHP WeChat development: How to implement message encryption and decryption May 13, 2023 am 11:40 AM

PHP WeChat development: How to implement message encryption and decryption

PHP WeChat development: How to implement customer service chat window management PHP WeChat development: How to implement customer service chat window management May 13, 2023 pm 05:51 PM

PHP WeChat development: How to implement customer service chat window management

PHP WeChat development: How to implement user tag management PHP WeChat development: How to implement user tag management May 13, 2023 pm 04:31 PM

PHP WeChat development: How to implement user tag management

PHP WeChat development: How to implement group message sending records PHP WeChat development: How to implement group message sending records May 13, 2023 pm 04:31 PM

PHP WeChat development: How to implement group message sending records

Using PHP to develop WeChat mass messaging tools Using PHP to develop WeChat mass messaging tools May 13, 2023 pm 05:00 PM

Using PHP to develop WeChat mass messaging tools

How to use PHP for WeChat development? How to use PHP for WeChat development? May 21, 2023 am 08:37 AM

How to use PHP for WeChat development?

PHP WeChat development: How to implement speech recognition PHP WeChat development: How to implement speech recognition May 13, 2023 pm 09:31 PM

PHP WeChat development: How to implement speech recognition

ThinkPHP6 WeChat Development Guide: Quickly build WeChat public account applications ThinkPHP6 WeChat Development Guide: Quickly build WeChat public account applications Aug 26, 2023 pm 11:55 PM

ThinkPHP6 WeChat Development Guide: Quickly build WeChat public account applications

See all articles