2.用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket(有效期7200秒,开发者必须在自己的服务全局缓存jsapi_ticket):https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
获得jsapi_ticket之后,就可以生成JS-SDK权限验证的签名了。
第三步生成JS-SDK权限验证的签名
签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分) 。对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。
注意事项
1.签名用的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同;
2.签名用的url必须是调用JS接口页面的完整URL;
3.出于安全考虑,开发者必须在服务器端实现签名的逻辑。
以上便是微信JS SDK的签名逻辑部分,下面奉上具体的代码实现:
using System; using System.Net; using System.Web.Mvc; using System.IO; using System.Text; using System.Web.Script.Serialization; using ManYiAbyWAP.Models; 微信获取jsapi_ticket ManYiAbyWAP.Controllers { public class WXMessageController : Controller { APPID = ; SECRECT = "微信提供的secrect"; DateTime overTime = DateTime.Now;ActionResult GetToken(string url) { if (accessToken != null) { TimeSpan span = Convert.ToDateTime(overTime).Subtract(Convert.ToDateTime(DateTime.Now)); if (span.TotalHours > 2) { accessToken = GETWinXinToken(); overTime = DateTime.Now; } } else { accessToken = GETWinXinToken(); overTime = DateTime.Now; } //生成签名的时间戳 TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); accessToken.timestamp = Convert.ToInt64(ts.TotalSeconds).ToString(); //获取随机串 accessToken.noncestr = createNonceStr(16); + accessToken .ticket+ + accessToken.noncestr + + accessToken.timestamp + + url; accessToken.signature = SHA1(str).ToLower(); accessToken.APPID = APPID; return Json(accessToken, JsonRequestBehavior.AllowGet); } createNonceStr(int length = 16) { ; Random r = new Random(); string result = string.Empty; (int i = 0; i < length; i++) { s = str.Substring(m, 1); result += s; } return result; } SHA1(string text) { byte[] cleanBytes = Encoding.Default.GetBytes(text); byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes); , ""); } WXInfo GETWinXinToken() { //获取access_token HttpWebResponse response = CreateGetHttpResponse(+ APPID + + SECRECT, 5000); StreamReader reader = new StreamReader(response.GetResponseStream()); String line; StringBuilder sb = new StringBuilder(); while ((line = reader.ReadLine()) != null) { sb.Append(line.ToString()); } JavaScriptSerializer js = new JavaScriptSerializer(); WXInfo accessToken = js.Deserialize<WXInfo>(sb.ToString()); //获取jsapi_ticket response = CreateGetHttpResponse(+ accessToken.access_token + , 5000); reader = new StreamReader(response.GetResponseStream()); sb = new StringBuilder(); while ((line = reader.ReadLine()) != null) { sb.Append(line.ToString()); } accessToken = js.Deserialize<WXInfo>(sb.ToString()); return accessToken; } 创建GET方式的HTTP请求 HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent=null, CookieCollection cookies=null) { if (string.IsNullOrEmpty(url)) { ); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = ; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } return request.GetResponse() as HttpWebResponse; } } }
用于存放Token信息的全局对象WXInfo.cs