search
HomeWeChat AppletWeChat DevelopmentASP.NET WeChat Development Interface Guide Detailed Introduction

ASP.NET WeChat Development Interface Guide Detailed Introduction

Mar 10, 2017 pm 02:34 PM
asp.netWeChat 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(); 
 }


## 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) 
 { 
  
 } 
 }

 

 

 

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
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment