欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET微信开发(接口指南)

程序员文章站 2023-12-20 23:23:43
公众平台用户提交信息后,微信服务器将发送get请求到填写的url上,并且带上四个参数: 开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次g...

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

ASP.NET微信开发(接口指南)

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

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

加密/校验流程:

  • 1. 将token、timestamp、nonce三个参数进行字典序排序
  • 2. 将三个参数字符串拼接成一个字符串进行sha1加密
  • 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
/// <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(); 
 } 

接入后是消息推送当普通微信用户向公众账号发消息时,微信服务器将post该消息到填写的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) 
 { 
  
 } 
 }</pre><br> 
<pre></pre> 
<br> 
<br> 

本文已被整理到了《asp.net微信开发教程汇总》,欢迎大家学习阅读。

以上就是关于asp.net微信开发接口指南的相关内容介绍,希望对大家的学习有所帮助。

上一篇:

下一篇: