微信公众平台接入方式(asp.net)
第一步:使用vs2012等等建一个web站点
1.1 建立完成后,新建一个接入的页面,以aspx为例
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; namespace weixin.weixin { /// <summary> /// 接入微信公众平台 /// </summary> public partial class weixin_api : System.Web.UI.Page { /// <summary> /// 此token(票据)和服务器一致即可,可以随便写 /// </summary> private const string token = "mmmv390crazyhs"; //是从微信服务器发来的url,并不是某人访问浏览器的url protected void Page_Load(object sender, EventArgs e) { string url = Request.Url.ToString(); WriteLog("url:"+url); //开始验证 Valid(); string postStr = ""; if (Request.HttpMethod.ToLower() == "post") { Stream s = System.Web.HttpContext.Current.Request.InputStream; byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); postStr = Encoding.UTF8.GetString(b); if (!string.IsNullOrEmpty(postStr)) { ResponseMsg(postStr); } //WriteLog("postStr:" + postStr); } } /// <summary> /// 2.0 调用CheckSignature方法验证签名是否正确,如果正确,就把从微信服务器上发来的ehcostr再返回给微信服务器,这样就微信公众平台服务器就知道接入成功了 /// </summary> /// * /// * /// * public void Valid() { string echostr = Request.Params["echostr"]; if (CheckSignature()) { //如果微信签名验证成功 if (!string.IsNullOrEmpty(echostr)) { WriteLog("echoStr不为null:" + echostr); Response.Write(echostr); Response.End();; } } } /// <summary> /// 1.0 验证微信签名[应该是防止被人在网络上嗅探到明文的 token(票据)] /// </summary> ///* 将token、timestamp、nonce三个参数进行字典序排序 ///* 将三个参数字符串拼接成一个字符串进行sha1加密 ///*开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 /// http://www.rdpsafe.com/weixin/weixin_api.aspx?timestamp=1421492404&nonce=1067705734&signature=7da3235a00c03a14105741dbb306e193871be8fb ///<returns></returns> public bool CheckSignature() { bool res = false; //1.1 接收微信服务器发来的签名,用来做最后的比较 string signatureFromWeixinServ = Request.Params["signature"]; //1.2 接收timestamp string timestamp = Request.Params["timestamp"]; //1.3 接收nonce string nonce = Request.Params["nonce"]; //1.4 开始用timestamp和nonce来组装加密成signature来跟接收到的signature比较 //1.4.1 合并三个字符串到字符串数组中 string[] arrStrs = { token, timestamp, nonce }; //1.4.2 排序 Array.Sort(arrStrs); //1.4.3 接收返回的字符串 string resStr = string.Join("", arrStrs); //调试 WriteLog("token:" + token); WriteLog("timestamp:" + timestamp); WriteLog("nonce:" + nonce); WriteLog("返回的是:" + resStr); WriteLog("提交来的签名:"+signatureFromWeixinServ); //1.4.4 使用sha1加密算法加密 resStr = FormsAuthentication.HashPasswordForStoringInConfigFile(resStr, "SHA1"); //1.4.5 变成小写 resStr = resStr.ToLower(); //1.5 比较后返回 if (resStr.Equals(signatureFromWeixinServ)) { res = true; } else { res = false; } return res; } /// <summary> /// 3.0 返回信息结果(微信信息返回) /// </summary> /// <param name="weixinXML"></param> private void ResponseMsg(string weixinXML) { //回复消息的部分:你的代码写在这里 } public void WriteLog(string log) { string filename = Server.MapPath("/logs/log.txt"); if (!Directory.Exists(Server.MapPath("//logs//"))) Directory.CreateDirectory(Server.MapPath("/")+"//logs//"); StreamWriter sr = null; try { if (!File.Exists(filename)) { sr = File.CreateText(filename); } else { sr = File.AppendText(filename); } sr.WriteLine(log); } catch { } finally { if (sr != null) sr.Close(); } } /// <summary> /// datetime转换为unixtime /// </summary> /// <param name="time"></param> /// <returns></returns> private int ConvertDateTimeInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } } }
第二步: 如果没有安装IIS,以本机为例,先安装一个iis
2.1 安装iis,以win8.1为例 ,快捷键CTRL+X,在弹出的选项卡中选择"程序和功能",在弹出的窗口中点击"启用和关闭window功能"
2.2在iis中新建一个网站,并把目录指向vs2012中网站项目的根目录即可
这样就OK了
原文地址:http://blog.csdn.net/mmmv390/article/details/42808125