获取微信签名
private void SYS()
{
string timpstamps = WeHelper.CreateTimestamp();
string nonceStr = WeHelper.CreateNonceStr();
string url = Context.Request.Url.ToString();
string sig = WeHelper.GetSignature(timpstamps, nonceStr, url);
var json = new
{
appId = WeHelper.appId,
timpstamp = timpstamps,
noncestr = nonceStr,
signature = sig
};
string _Result = new JavaScriptSerializer().Serialize(json);
}
public static class WeHelper
{
public static string appId
{
get
{
return Wxinterface.wxapp.appid;
}
}
private static string secret
{
get
{
return Wxinterface.wxapp.secret;
}
}
private static readonly string tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
private static readonly string ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
public static string GetAccessToken()
{
if (HttpRuntime.Cache.Get("AccessToken") == null)
{
StringBuilder sbUrl = new StringBuilder();
sbUrl.AppendFormat(tokenUrl);
sbUrl.AppendFormat("?grant_type=client_credential");
sbUrl.AppendFormat("&appid={0}", appId);
sbUrl.AppendFormat("&secret={0}", secret);
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
string data = client.UploadString(sbUrl.ToString(), string.Empty);
var result = JObject.Parse(data);
if (result["access_token"] != null && result["access_token"].Value<string>() != string.Empty)
{
string token = result["access_token"].Value<string>();
HttpRuntime.Cache.Insert("AccessToken", token, null, DateTime.Now.AddSeconds(7200), System.Web.Caching.Cache.NoSlidingExpiration);
return token;
}
else
throw new Exception(data);
}
}
else
{
return HttpRuntime.Cache.Get("AccessToken").ToString();
}
}
public static string GetTicket(string accessToken)
{
if (HttpRuntime.Cache.Get("Ticket") == null)
{
StringBuilder sbUrl = new StringBuilder();
sbUrl.AppendFormat(ticket);
sbUrl.AppendFormat("?access_token={0}", accessToken);
sbUrl.AppendFormat("&type=jsapi");
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
string data = client.UploadString(sbUrl.ToString(), string.Empty);
var result = JObject.Parse(data);
if (result["ticket"] != null && result["ticket"].Value<string>() != string.Empty)
{
string ticket1 = result["ticket"].Value<string>();
HttpRuntime.Cache.Insert("Ticket", ticket1, null, DateTime.Now.AddSeconds(7200), System.Web.Caching.Cache.NoSlidingExpiration);
return ticket1;
}
else
throw new Exception(data);
}
}
else
{
return HttpRuntime.Cache.Get("Ticket").ToString();
}
}
public static string GetSignature(string timestamp, string noncestr, string url)
{
string string1 = "jsapi_ticket=" + GetTicket(GetAccessToken()) + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url;
return SHA1(string1);
}
#region 工具类
public static string CreateNonceStr()
{
int length = 16;
string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string str = "";
var rad = new Random();
for (int i = 0; i < length; i++)
{
str += chars.Substring(rad.Next(0, chars.Length - 1), 1);
}
return str;
}
public static string SHA1(string str)
{
str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToString();
return str.ToLower();
}
public static string CreateTimestamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
return timestamp;
}
#endregion
}