asp.net微信公众平台开发

http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97 

微信公众平台接口指南

微信公众平台的开发比较简单,首先是网址接入

公众平台用户提交信息后,微信服务器将发送GET请求到填写的URL上,并且带上四个参数:

参数描述
signature 微信加密签名
timestamp 时间戳
nonce 随机数
echostr 随机字符串

开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。

signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

加密/校验流程:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
[csharp] view plaincopy
 
  1. /// <summary>  
  2.  /// 验证签名  
  3.  /// </summary>  
  4.  /// <param name="signature"></param>  
  5.  /// <param name="timestamp"></param>  
  6.  /// <param name="nonce"></param>  
  7.  /// <returns></returns>  
  8.  public static bool CheckSignature(String signature, String timestamp, String nonce)  
  9.  {  
  10.      String[] arr = new String[] { token, timestamp, nonce };  
  11.      // 将token、timestamp、nonce三个参数进行字典序排序    
  12.      Array.Sort<String>(arr);  
  13.   
  14.      StringBuilder content = new StringBuilder();  
  15.      for (int i = 0; i < arr.Length; i++)  
  16.      {  
  17.          content.Append(arr[i]);  
  18.      }  
  19.   
  20.      String tmpStr = SHA1_Encrypt(content.ToString());  
  21.   
  22.   
  23.      // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信    
  24.      return tmpStr != null ? tmpStr.Equals(signature) : false;  
  25.  }  
  26.   
  27.   
  28.  /// <summary>  
  29.  /// 使用缺省密钥给字符串加密  
  30.  /// </summary>  
  31.  /// <param name="Source_String"></param>  
  32.  /// <returns></returns>  
  33.  public static string SHA1_Encrypt(string Source_String)  
  34.  {  
  35.      byte[] StrRes = Encoding.Default.GetBytes(Source_String);  
  36.      HashAlgorithm iSHA = new SHA1CryptoServiceProvider();  
  37.      StrRes = iSHA.ComputeHash(StrRes);  
  38.      StringBuilder EnText = new StringBuilder();  
  39.      foreach (byte iByte in StrRes)  
  40.      {  
  41.          EnText.AppendFormat("{0:x2}", iByte);  
  42.      }  
  43.      return EnText.ToString();  
  44.  }  

接入后是消息推送当普通微信用户向公众账号发消息时,微信服务器将POST该消息到填写的URL上。
[csharp] view plaincopy
 
  1.     protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.   
  4.         if (Request.HttpMethod.ToUpper() == "GET")  
  5.         {  
  6.             // 微信加密签名    
  7.             string signature = Request.QueryString["signature"];  
  8.             // 时间戳    
  9.             string timestamp = Request.QueryString["timestamp"];  
  10.             // 随机数    
  11.             string nonce = Request.QueryString["nonce"];  
  12.             // 随机字符串    
  13.             string echostr = Request.QueryString["echostr"];  
  14.             if (WeixinServer.CheckSignature(signature, timestamp, nonce))  
  15.             {  
  16.                 Response.Write(echostr);  
  17.             }  
  18.   
  19.         }  
  20.         else if (Request.HttpMethod.ToUpper() == "POST")  
  21.         {  
  22.   
  23.            StreamReader stream = new StreamReader(Request.InputStream);  
  24.            string xml = stream.ReadToEnd();  
  25.   
  26.            processRequest(xml);  
  27.         }  
  28.   
  29.    
  30.     }  
  31.   
  32.   
  33.     /// <summary>  
  34.     /// 处理微信发来的请求   
  35.     /// </summary>  
  36.     /// <param name="xml"></param>  
  37.     public void processRequest(String xml)  
  38.     {  
  39.         try  
  40.         {  
  41.   
  42.             // xml请求解析    
  43.             Hashtable requestHT = WeixinServer.ParseXml(xml);  
  44.   
  45.             // 发送方帐号(open_id)    
  46.             string fromUserName = (string)requestHT["FromUserName"];  
  47.             // 公众帐号    
  48.             string toUserName = (string)requestHT["ToUserName"];  
  49.             // 消息类型    
  50.             string msgType = (string)requestHT["MsgType"];  
  51.   
  52.             //文字消息  
  53.             if (msgType == ReqMsgType.Text)  
  54.             {  
  55.                // Response.Write(str);  
  56.   
  57.                string content = (string)requestHT["Content"];  
  58.                 if(content=="1")  
  59.                 {  
  60.                      // Response.Write(str);  
  61.                     Response.Write(GetNewsMessage(toUserName, fromUserName));  
  62.                     return;  
  63.                 }  
  64.                 if (content == "2")  
  65.                 {  
  66.                     Response.Write(GetUserBlogMessage(toUserName, fromUserName));  
  67.                      return;  
  68.                 }  
  69.                 if (content == "3")  
  70.                 {  
  71.                     Response.Write(GetGroupMessage(toUserName, fromUserName));  
  72.                    return;  
  73.                 }  
  74.                 if (content == "4")  
  75.                 {  
  76.                     Response.Write(GetWinePartyMessage(toUserName, fromUserName));  
  77.                     return;  
  78.                 }  
  79.                 Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,"));   
  80.   
  81.            }  
  82.            else if (msgType == ReqMsgType.Event)  
  83.            {  
  84.                // 事件类型    
  85.                String eventType = (string)requestHT["Event"];  
  86.                // 订阅    
  87.                if (eventType==ReqEventType.Subscribe)  
  88.                {  
  89.                      
  90.                    Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,"));  
  91.                      
  92.                }  
  93.                // 取消订阅    
  94.                else if (eventType==ReqEventType.Unsubscribe)  
  95.                {  
  96.                    // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息    
  97.                }  
  98.                // 自定义菜单点击事件    
  99.                else if (eventType==ReqEventType.CLICK)  
  100.                {  
  101.                    // TODO 自定义菜单权没有开放,暂不处理该类消息    
  102.                }    
  103.            }  
  104.            else if (msgType == ReqMsgType.Location)  
  105.            {  
  106.            }  
  107.   
  108.     
  109.         }  
  110.         catch (Exception e)  
  111.         {  
  112.               
  113.         }  
  114.     }<pre name="code" class="csharp">    protected void Page_Load(object sender, EventArgs e)  
  115.     {  
  116.   
  117.         if (Request.HttpMethod.ToUpper() == "GET")  
  118.         {  
  119.             // 微信加密签名    
  120.             string signature = Request.QueryString["signature"];  
  121.             // 时间戳    
  122.             string timestamp = Request.QueryString["timestamp"];  
  123.             // 随机数    
  124.             string nonce = Request.QueryString["nonce"];  
  125.             // 随机字符串    
  126.             string echostr = Request.QueryString["echostr"];  
  127.             if (WeixinServer.CheckSignature(signature, timestamp, nonce))  
  128.             {  
  129.                 Response.Write(echostr);  
  130.             }  
  131.   
  132.         }  
  133.         else if (Request.HttpMethod.ToUpper() == "POST")  
  134.         {  
  135.   
  136.            StreamReader stream = new StreamReader(Request.InputStream);  
  137.            string xml = stream.ReadToEnd();  
  138.   
  139.            processRequest(xml);  
  140.         }  
  141.   
  142.    
  143.     }  
  144.   
  145.   
  146.     /// <summary>  
  147.     /// 处理微信发来的请求   
  148.     /// </summary>  
  149.     /// <param name="xml"></param>  
  150.     public void processRequest(String xml)  
  151.     {  
  152.         try  
  153.         {  
  154.   
  155.             // xml请求解析    
  156.             Hashtable requestHT = WeixinServer.ParseXml(xml);  
  157.   
  158.             // 发送方帐号(open_id)    
  159.             string fromUserName = (string)requestHT["FromUserName"];  
  160.             // 公众帐号    
  161.             string toUserName = (string)requestHT["ToUserName"];  
  162.             // 消息类型    
  163.             string msgType = (string)requestHT["MsgType"];  
  164.   
  165.             //文字消息  
  166.             if (msgType == ReqMsgType.Text)  
  167.             {  
  168.                // Response.Write(str);  
  169.   
  170.                string content = (string)requestHT["Content"];  
  171.                 if(content=="1")  
  172.                 {  
  173.                      // Response.Write(str);  
  174.                     Response.Write(GetNewsMessage(toUserName, fromUserName));  
  175.                     return;  
  176.                 }  
  177.                 if (content == "2")  
  178.                 {  
  179.                     Response.Write(GetUserBlogMessage(toUserName, fromUserName));  
  180.                      return;  
  181.                 }  
  182.                 if (content == "3")  
  183.                 {  
  184.                     Response.Write(GetGroupMessage(toUserName, fromUserName));  
  185.                    return;  
  186.                 }  
  187.                 if (content == "4")  
  188.                 {  
  189.                     Response.Write(GetWinePartyMessage(toUserName, fromUserName));  
  190.                     return;  
  191.                 }  
  192.                 Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好,我是vinehoo,"));   
  193.   
  194.            }  
  195.            else if (msgType == ReqMsgType.Event)  
  196.            {  
  197.                // 事件类型    
  198.                String eventType = (string)requestHT["Event"];  
  199.                // 订阅    
  200.                if (eventType==ReqEventType.Subscribe)  
  201.                {  
  202.                      
  203.                    Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注!,"));  
  204.                      
  205.                }  
  206.                // 取消订阅    
  207.                else if (eventType==ReqEventType.Unsubscribe)  
  208.                {  
  209.                    // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息    
  210.                }  
  211.                // 自定义菜单点击事件    
  212.                else if (eventType==ReqEventType.CLICK)  
  213.                {  
  214.                    // TODO 自定义菜单权没有开放,暂不处理该类消息    
  215.                }    
  216.            }  
  217.            else if (msgType == ReqMsgType.Location)  
  218.            {  
  219.            }  
  220.   
  221.     
  222.         }  
  223.         catch (Exception e)  
  224.         {  
  225.               
  226.         }  
  227.     }</pre><br>  
  228. <pre></pre>  
  229. <br>  
  230. <br>  
原文地址:https://www.cnblogs.com/Alex80/p/4236062.html