獲取微信簽名,並保存在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"));
            }        
        }
    }
}
相關文章
相關標籤/搜索