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

asp.net微信开发(开发者接入)

程序员文章站 2024-02-12 14:35:28
先上图,看一看需要进行哪些项目的操作:   在项目的根目录或者特定的文件夹内,创建一个ashx文件(一般处理程序文件),如图 public v...

先上图,看一看需要进行哪些项目的操作:

 asp.net微信开发(开发者接入)

在项目的根目录或者特定的文件夹内,创建一个ashx文件(一般处理程序文件),如图

 public void processrequest(httpcontext context)
 {
  context.response.contenttype = "text/plain";
  
  string poststring = string.empty;
  if (httpcontext.current.request.httpmethod.toupper() == "post")
  {
  using (stream stream = httpcontext.current.request.inputstream)
  {
   byte[] postbytes = new byte[stream.length];
   stream.read(postbytes, 0, (int32)stream.length);
   poststring = encoding.utf8.getstring(postbytes);
  }

  if (!string.isnullorempty(poststring))
  {
   responsexml(poststring);//返回给微信用户信息
  }

  ///加载自定义菜单
  string postdata = "{" + "\r\n";
  postdata += "\"button\":[ " + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += "\"name\":\"简单查\"," + "\r\n";
  postdata += "\"sub_button\":[" + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"我的薪资\", " + "\r\n";
  postdata += " \"key\":\"mypay\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"天气预报\", " + "\r\n";
  postdata += " \"key\":\"tianqiyubao\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"view\"," + "\r\n";
  postdata += " \"name\":\"火车票查询\", " + "\r\n";
  postdata += " \"url\":\"http://www.deqiaohr.com.cn/*******.aspx\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"开心一刻\", " + "\r\n";
  postdata += " \"key\":\"kaixinyixiao\"" + "\r\n";
  postdata += " }]" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{" + "\r\n";
  postdata += "\"name\":\"会员管理\", " + "\r\n";
  postdata += "\"sub_button\":[" + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"view\"," + "\r\n";
  postdata += " \"name\":\"会员注册\", " + "\r\n";
  postdata += " \"url\":\"http://www.deqiaohr.com.cn/****.aspx\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"view\"," + "\r\n";
  postdata += " \"name\":\"重置密码\", " + "\r\n";
  postdata += " \"url\":\"http://www.deqiaohr.com.cn/****.aspx\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"修改资料\", " + "\r\n";
  postdata += " \"key\":\"updatemessage\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"我的提问\", " + "\r\n";
  postdata += " \"key\":\"mywen\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"联系客服\", " + "\r\n";
  postdata += " \"key\":\"phoneserices\"" + "\r\n";
  postdata += " }]" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{" + "\r\n";
  postdata += "\"name\":\"活动通知\"," + "\r\n";
  postdata += "\"sub_button\":[" + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"近期活动\", " + "\r\n";
  postdata += " \"key\":\"yuangonghuodong\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"近期通知\", " + "\r\n";
  postdata += " \"key\":\"yuangongtongzhi\"" + "\r\n";
  postdata += "}," + "\r\n";
  postdata += "{ " + "\r\n";
  postdata += " \"type\":\"click\"," + "\r\n";
  postdata += " \"name\":\"有问必答\", " + "\r\n";
  postdata += " \"key\":\"youwenbida\"" + "\r\n";
  postdata += " }]" + "\r\n";
  postdata += "}]" + "\r\n";
  postdata += "}" + "\r\n";

  //自定义菜单token的获取 是用 下面的两个参数 获取的 不能直接用 公众平台的token
  string to = getaccesstoken();
  //本人不喜欢 后台 json的操作 直接截取就可以了 得到的就是 token 或者 自己 获取 json的token
  to = to.substring(17, to.length - 37);
  //加载菜单
  string i = getpage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + to, postdata);

  }
  else
  {
  auth(); //微信接入的测试
  }
  
 }

 

 /// <summary>
 /// 获取通行证
 /// </summary>
 /// <returns></returns>
 private string getaccesstoken()
 {
  string url_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=********&secret=*********";
  httpwebrequest myrequest = (httpwebrequest)webrequest.create(url_token);
  myrequest.method = "get";
  httpwebresponse myresponse = (httpwebresponse)myrequest.getresponse();
  streamreader reader = new streamreader(myresponse.getresponsestream(), encoding.utf8);
  string content = reader.readtoend();
  reader.close();
  return content;
 }

 

 /// <summary>
 /// 加载菜单项
 /// </summary>
 /// <param name="p"></param>
 /// <param name="postdata"></param>
 /// <returns></returns>
 private string getpage(string p, string postdata)
 {
  stream outstream = null;
  stream instream = null;
  streamreader sr = null;
  httpwebresponse response = null;
  httpwebrequest request = null;
  encoding encoding = encoding.utf8;
  byte[] data = encoding.getbytes(postdata);
  // 准备请求...
  try
  {
  // 设置参数
  request = webrequest.create(p) as httpwebrequest;
  cookiecontainer cookiecontainer = new cookiecontainer();
  request.cookiecontainer = cookiecontainer;
  request.allowautoredirect = true;
  request.method = "post";
  request.contenttype = "application/x-www-form-urlencoded";
  request.contentlength = data.length;
  outstream = request.getrequeststream();
  outstream.write(data, 0, data.length);
  outstream.close();
  //发送请求并获取相应回应数据
  response = request.getresponse() as httpwebresponse;
  //直到request.getresponse()程序才开始向目标网页发送post请求
  instream = response.getresponsestream();
  sr = new streamreader(instream, encoding);
  //返回结果网页(html)代码
  string content = sr.readtoend();
  string err = string.empty;
  return content;
  }
  catch (exception ex)
  {
  string err = ex.message;
  return string.empty;
  }
 }

 

 /// <summary>
 /// 获取参数进行认证
 /// </summary>
 private void auth()
 {
  string token = "*****";//你申请的时候填写的token

  string echostring = httpcontext.current.request.querystring["echostr"];
  string signature = httpcontext.current.request.querystring["signature"];
  string timestamp = httpcontext.current.request.querystring["timestamp"];
  string nonce = httpcontext.current.request.querystring["nonce"];

  if (checksignature(token, signature, timestamp, nonce))
  {
  if (!string.isnullorempty(echostring))
  {
   httpcontext.current.response.write(echostring);
   httpcontext.current.response.end();
  }
  }

 }
 /// <summary>
 /// 对微信传入参数进行封装到数组,拼接字符串,进行加密操作
 /// </summary>
 /// <param name="token"></param>
 /// <param name="signature"></param>
 /// <param name="timestamp"></param>
 /// <param name="nonce"></param>
 /// <returns></returns>
 private bool checksignature(string token, string signature, string timestamp, string nonce)
 {
  string[] arrtmp = { token, timestamp, nonce };//将参数放进数组

  array.sort(arrtmp);//对数组进行排序
  string tmpstr = string.join("", arrtmp);//将数组进行拼接
  ///对拼接后的字符串进行加密操作
  tmpstr = formsauthentication.hashpasswordforstoringinconfigfile(tmpstr, "sha1");
  //转换成小写形式
  tmpstr = tmpstr.tolower();
  //比对成功返回
  if (tmpstr == signature)
  {
  return true;
  }
  else
  {
  return false;
  }
 }

精彩专题分享:asp.net微信开发教程汇总,欢迎大家学习。

以上就是关于asp.net微信开发的第一篇,针对开发者接入进行学习,之后会有更新更多关于asp.net微信开发的文章,希望大家持续关注。