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微信开发的文章,希望大家持续关注。

(0)

相关推荐

  • MVC微信网页授权获取用户OpenId

    最近开发微信公众平台,做下记录,以前也开发过,这次开发又给忘了,搞了半天,还是做个笔记为好. 注意框架为MVC 开发微信公众平台.场景为,在模板页中获取用户openid,想要进行验证的页面,集成模板页就可以了. 在_Layout.cshtml中加入如下代码 <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, ini

  • 微信公众号支付(一)如何获取用户openId

    一.获取apikey,appsecret与商户号 注册公众号.商户号 二.获取用户的OpenId 1.设置[授权回调页面域名] 官方解释:用户在网页授权页同意授权给公众号后,微信会将授权数据传给一个回调页面,回调页面需在此域名下,以确保安全可靠.回调页面域名不支持IP地址. 2.用户同意授权 我是把这个url写在微信菜单下的,当进入这个页面的时候就让用户同意.注意:好像是静默授权的,用户不知道 1.url: https://open.weixin.qq.com/connect/oauth/aut

  • .NET微信公众号获取OpenID和用户信息

    本文实例为大家分享了微信公众平台实现获取用户OpenID的方法,供大家参考,具体内容如下 Index.aspx.cs代码: public partial class Index : System.Web.UI.Page { //用户id public string openid = ""; //公众号信息部分 public string appid = ConfigurationManager.AppSettings["AppId"]; public string

  • .net开发微信公众平台实例教程

    本文实例讲述了.net开发微信公众平台的方法.分享给大家供大家参考.具体实现方法如下: 一.说明: 公众平台信息接口为开发者提供了一种新的消息处理方式,只有申请成为开发者后,你才能使用公众平台的开发功能,在这里你需要填写一个URL和一个Token,这两项信息也需要你拥有自己的服务器(外网服务器)资源,其中的Token可由开发者任意填写,URL即是接口配置信息的链接地址,在本文中我采用的是创建一个简易网站的方式,在其中的一个页面的后台程序中配置相关的接口信息,然后发布到外网服务器上,最后可以访问到

  • .net实现微信公众账号接口开发实例代码

    说起微信公众帐号,大家都不会陌生,使用这个平台能给网站或系统增加一个新亮点,直接进入正题吧,在使用之前一定要仔细阅读官方API文档.API文档地址:http://mp.weixin.qq.com/wiki/index.php 使用.net实现的方法://微信接口地址 页面代码: 复制代码 代码如下: weixin _wx = new weixin();  string postStr = "";  if (Request.HttpMethod.ToLower() == "po

  • 微信小程序 获取微信OpenId详解及实例代码

    获取微信OpenId 先获取code 再通过code获取authtoken,从authtoken中取出openid给前台 微信端一定不要忘记设定网页账号中的授权回调页面域名 流程图如下 主要代码 页面js代码 /* 写cookie */ function setCookie(name, value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); doc

  • PHP通过微信跳转的Code参数获取用户的openid(关键代码)

    关键代码如下所示: //获取微信登录用户信息 function getOpenID($appid,$appsecret,$code){ $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=". $appsecret."&code=".$code."&grant_type=authorization_code

  • asp.net微信开发(永久素材管理)

    除了3天就会失效的临时素材外,开发者有时需要永久保存一些素材,届时就可以通过本接口新增永久素材. 最近更新,永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用,图片将被屏蔽). 请注意: 1.新增的永久素材也可以在公众平台官网素材管理模块中看到 2.永久素材的数量是有上限的,请谨慎新增.图文消息素材和图片素材的上限为5000,其他类型为1000 3.素材的格式大小等要求与公众平台官网一致.具体是,图片大小不超过2M,支持bmp/png/jpeg/jpg/

  • asp.net开发微信公众平台之获取用户消息并处理

    获取用户消息 用户发送的消息是在微信服务器发送的一个HTTP POST请求中包含的,获取用户发送的消息要从POST请求的数据流中获取 微信服务器推送消息到服务器的HTTP请求报文示例 POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6&timestamp=1409659813&nonce=1372623149 HTTP/1.1 Host: qy.weixin.qq.com 从POST请求中

  • 微信公众平台实现获取用户OpenID的方法

    本文实例讲述了微信公众平台实现获取用户OpenID的方法.分享给大家供大家参考.具体分析如下: 用户点击微信自定义菜单view类型按钮后,微信客户端将会打开开发者在按钮中填写的url值 (即网页链接),达到打开网页的目的,但是view不能获取用户的openid,需要使用微信"网页授权获取用户基本信息"高级接口结合使用,获得用户的登入个人信息. 具体方法: 1.配置网页授权回调域名,如 www.jb51.net 2.模拟公众号的第三方网页,http://www.jb51.net/getc

随机推荐