微信--获取jsapi_ticket 然后在计算出signature
最近做微信公众号,记录一下,仅供参看。
关于jsapi_ticket微信公众号有相关说明:
生成签名之前必须先了解一下jsapi_ticket,jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。由于获取jsapi_ticket的api调用次数非常有限,频繁刷新jsapi_ticket会导致api调用受限,影响自身业务,开发者必须在自己的服务全局缓存jsapi_ticket 。
Ticket
1 public class Ticket
2 {
3 public string Value { get; set; }
4 public DateTime Time { get; set; }
5 }
WeixinServer
1 #region 静态全局变量
2 private static Token _Token = null;
3 private static Ticket _Ticket = null;
4 private readonly static string appid = "wx123456789000";
5 private readonly static string secret = "abcdefghijklmnopqrstuvwxyz";
6 private readonly static string domain = "http://wechat.mydomain.com";
7 #endregion
1 public string GetTicket() 2 { 3 bool first = false; 4 if (_Ticket == null) 5 { 6 _Ticket = new Ticket(); 7 first = true; 8 } 9 TimeSpan ts = DateTime.Now - _Ticket.Time; 10 if (ts.Minutes > 30 || first) 11 { 12 string access_token = AccessToken(); 13 string url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi"; 14 string requstStr = HttpHelper.Get(url1); 15 string ticket = GetParameter("ticket", requstStr); 16 _Ticket.Value = ticket; 17 _Ticket.Time = DateTime.Now; 18 } 19 return _Ticket.Value; 20 }
1 //获取jssdk所需签名 2 public string Signature(string url) 3 { 4 string noncestr = "Wm3WZYTPz0wzccnW"; 5 int timestamp = 1414587457; 6 string ticket = GetTicket(); 7 8 string string1 = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url; 9 string signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string1, "SHA1"); 10 return signature; 11 }