微信--获取jsapi_ticket 然后在计算出signature

时间:2021-05-20 20:13:14

最近做微信公众号,记录一下,仅供参看。

关于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
         public string GetTicket()
{
bool first = false;
if (_Ticket == null)
{
_Ticket = new Ticket();
first = true;
}
TimeSpan ts = DateTime.Now - _Ticket.Time;
if (ts.Minutes > || first)
{
string access_token = AccessToken();
string url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";
string requstStr = HttpHelper.Get(url1);
string ticket = GetParameter("ticket", requstStr);
_Ticket.Value = ticket;
_Ticket.Time = DateTime.Now;
}
return _Ticket.Value;
}
         //获取jssdk所需签名
public string Signature(string url)
{
string noncestr = "Wm3WZYTPz0wzccnW";
int timestamp = ;
string ticket = GetTicket(); string string1 = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr + "&timestamp=" + timestamp + "&url=" + url;
string signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string1, "SHA1");
return signature;
}