侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

微信(小程序)  /  管理员 发布于 7年前   176

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

 

在项目的根目录或者特定的文件夹内,创建一个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微信开发的文章,希望大家持续关注。


  • 上一条:
    asp.net微信开发(消息应答)
    下一条:
    Android 微信文件传输助手文件夹
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客