微信公众平台开发之微信接入验证
摘要:微信,想必大家都不陌生,现在越来越多的微服务也开始进入我们的生活,我们可以使用微信进行查询账单、消费提醒、打车、还款、支付等等,最近微信官方又开放了很多高级接口,使得购买电影票、手机充值、收看互联网电视付费节目变成可能。
相要通过微信和自身业务进行紧密相连,就需要实现如何与微信服务器进行对接,涉足微信开发,首先需要到微信公众平台申请一个微信公众号,打开开发者模式后,首先需要对您填写的url和Token进行验证,验证url的有效性,是微信后续开发的首要条件。下面是实现url验证判断的代码: /// /// 微信URL验证 /// private void RenZheng() { #region 新认证 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 (CheckSignature(signature, timestamp, nonce)) { Response.Write(echostr); Response.End(); } } #endregion } public static bool CheckSignature(String signature, Stringtimestamp, String nonce) { string token = ViewUtil.WXToken; String[] arr = new String[] { token, timestamp, nonce }; // 将token、timestamp、nonce三个参数进行字典序排序 Array.Sort(arr); StringBuilder content = new StringBuilder(); for (int i = 0; i < arr.Length; i++) { content.Append(arr); } String tmpStr = SHA1_Encrypt(content.ToString()); // 将sha1加密后的字符串可与signature对比,标识该请求来源于微信 return tmpStr != null ? tmpStr.Equals(signature) : false; } /// /// 使用缺省密钥给字符串加密 /// /// /// 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(); } } 验证成功后,即可成为开发者,可以根据自身业务实现各种个性话的功能定制,比如智能回复,多客服,消息自动推送,位置定位等,多样化的功能使得自身系统变得更加贴近实用,为用户带来实实在在的益处!
http://www.wechatstyle.com/weixinkaifa/254.html
微信公众平台开发之微信接入验证
|