获取微信签名,并保存在xml文件中
using System; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Configuration; using Senparc.Weixin.MP.AdvancedAPIs; using Senparc.Weixin.MP.Helpers; using System.Xml; using Newtonsoft.Json.Linq; using BoLeBang.Phone.User; using System.IO; namespace BoLeBang.Phone.Shared { public partial class Dock : System.Web.UI.MasterPage { public string sinature,nonceStr,timeStamp; public string appID = ConfigurationManager.AppSettings["appID"]; public string apprecset = ConfigurationManager.AppSettings["apprecset"]; //设置xml文件保存路径 string path = HttpContext.Current.Server.MapPath("~/phone/jssdk.xml"); protected void Page_Load(object sender, EventArgs e) { //构造url地址,注意是包含域名的 C: string url = "http://ceshi.bng.org" + Request.RawUrl; if (File.Exists(path)) { //找到该xml文件 XmlDocument xml = new XmlDocument(); xml.Load(path); //获取xml文件节点的文本 string acces = xml.GetElementsByTagName("access_token")[0].InnerText; string time = xml.GetElementsByTagName("time")[0].InnerText; //2小时过期时间,获取时间间隔 DateTime dt = DateTime.Now; DateTime dt2 = Convert.ToDateTime(time); TimeSpan span = dt2 - dt; App_Code.Log.WriteLog(span.Hours.ToString()); if (span.Hours < 2) { //利用xml中的jsapi_ticket,来获取签名 ,利用的盛派sdk string jssdkticket = xml.GetElementsByTagName("jssdkTicket")[0].InnerText; nonceStr = JSSDKHelper.GetNoncestr(); timeStamp = JSSDKHelper.GetTimestamp(); sinature = JSSDKHelper.GetSignature(jssdkticket, nonceStr, timeStamp, url); }//低于两个小时 else { //这个应该是修改xml文件,我利用的是删除后重现建立 App_Code.Log.WriteLog("删除xml"); File.Delete(path); //重新判断 goto C; } }//如果存在xml文件 else { #region 获取access_token CheckLogin login = new CheckLogin(); string url11 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret=" + apprecset; JObject jj = login.GetMsg(url11, false); string accs = jj["access_token"].ToString(); #endregion #region //获取jsticket string hh = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accs + "&type=jsapi"; JObject jo = login.GetMsg(hh, false); string ticket = jo["ticket"].ToString(); #endregion CreateXML(accs, ticket); JsSdkUiPackage jssdkuipackage = JSSDKHelper.GetJsSdkUiPackage(appID, apprecset, url); nonceStr = jssdkuipackage.NonceStr; timeStamp = jssdkuipackage.Timestamp; sinature = jssdkuipackage.Signature; } } /// <summary> ///把access和jssdkticket存在xml文件中 /// </summary> /// <param name="access_token"></param> /// <param name="jssdkTicket"></param> void CreateXML(string access_token,string jssdkTicket) { if (!File.Exists(path)) //如果日志目录不存在就创建 { XmlDocument xd = new XmlDocument();//表示XML文档 XmlDeclaration xde;//表示 XML 声明节点:<?xml version='1.0'...?> xde = xd.CreateXmlDeclaration("1.0", "GBK", null);//参数的第二项为编码方式 //standalone定义了是否可以在不读取任何其它文件的情况下处理该文档,默认为no xd.AppendChild(xde);//<?xml version="1.0" encoding="UTF-8" standalone="yes"?>生成结束 XmlElement xe = xd.CreateElement("jssdk");//jssdk xd.AppendChild(xe);//Root根元素创建完成 XmlNode jssdk = xd.SelectSingleNode("jssdk");//查找<jssdk> XmlElement xe1 = xd.CreateElement("access_token");//在<jssdk>之下创建元素<access_token> XmlElement xe2 = xd.CreateElement("jssdkTicket"); XmlElement time = xd.CreateElement("time"); xe1.SetAttribute("id", "acces");//指定属性的属性值 xe1.InnerText = access_token;//指定属性文本节点 xe2.SetAttribute("id", "jssdkticket"); xe2.SetAttribute("value", jssdkTicket); xe2.InnerText = jssdkTicket; time.SetAttribute("id", "time"); time.InnerText=DateTime.Now.ToString(); //time.InnerText = ; jssdk.AppendChild(xe1);// jssdk.AppendChild(xe2);// jssdk.AppendChild(time);// xd.Save(Server.MapPath("jssdk.xml")); } } } }