微信 獲取wx.config 參數 基類
阿新 • • 發佈:2018-04-28
Coding ssa format style seconds tool pri temp ace
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Serialization; using System.Text; using System.Web.Caching; namespace Saturn.TTS.WMS.WeiXiPortal.HelperService { public class WXToolsHelper { /// <summary> /// 獲取全局的access_token,程序緩存/// </summary> /// <param name="AppId">第三方用戶唯一憑證</param> /// <param name="AppSecret">第三方用戶唯一憑證密鑰,即appsecret</param> /// <returns>得到的全局access_token</returns> public string Getaccess_token(string AppId, string AppSecret) { try { //先查緩存數據 if (HttpContext.Current.Cache["access_token"] != null) { return HttpContext.Current.Cache["access_token"].ToString(); } else { return Gettoken(AppId, AppSecret); } } catch { return Gettoken(AppId, AppSecret); } } /// <summary> /// 獲取全局的access_token /// </summary> /// <param name="AppId">第三方用戶唯一憑證</param>/// <param name="AppSecret">第三方用戶唯一憑證密鑰,即appsecret</param> /// <returns>得到的全局access_token</returns> public string Gettoken(string AppId, string AppSecret) { var client = new System.Net.WebClient(); client.Encoding = System.Text.Encoding.UTF8; var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppId, AppSecret); var data = client.DownloadString(url); var jss = new JavaScriptSerializer(); var access_tokenMsg = jss.Deserialize<Dictionary<string, object>>(data); //放入緩存中 HttpContext.Current.Cache.Insert("access_token", access_tokenMsg["access_token"], null, DateTime.Now.AddSeconds(7100), TimeSpan.Zero, CacheItemPriority.Normal, null); //清除jsapi_ticket緩存 HttpContext.Current.Cache.Remove("ticket"); //獲取jsapi_ticket,為了同步 GetTicket(access_tokenMsg["access_token"].ToString()); return access_tokenMsg["access_token"].ToString(); } /// <summary> /// 獲取jsapi_ticket,程序緩存 /// </summary> /// <param name="access_token">全局的access_token</param> /// <returns>得到的jsapi_ticket</returns> public string GetJsapi_Ticket(string access_token) { try { //先查緩存數據 if (HttpContext.Current.Cache["ticket"] != null) { return HttpContext.Current.Cache["ticket"].ToString(); } else { return GetTicket(access_token); } } catch { return GetTicket(access_token); } } /// <summary> /// 獲取jsapi_ticket /// </summary> /// <param name="access_token">全局的access_token</param> /// <returns>得到的jsapi_ticket</returns> public string GetTicket(string access_token) { var client = new System.Net.WebClient(); client.Encoding = System.Text.Encoding.UTF8; var url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token); var data = client.DownloadString(url); var jss = new JavaScriptSerializer(); var ticketMsg = jss.Deserialize<Dictionary<string, object>>(data); try { //放入緩存中 HttpContext.Current.Cache.Insert("ticket", ticketMsg["ticket"], null, DateTime.Now.AddSeconds(7100), TimeSpan.Zero, CacheItemPriority.Normal, null); return ticketMsg["ticket"].ToString(); } catch (Exception ex) { return ex.Message; } } /// <summary> /// 微信權限簽名的 sha1 算法 /// 簽名用的noncestr和timestamp必須與wx.config中的nonceStr和timestamp相同 /// </summary> /// <param name="jsapi_ticket">獲取到的jsapi_ticket</param> /// <param name="noncestr">生成簽名的隨機串</param> /// <param name="timestamp">生成簽名的時間戳</param> /// <param name="url">簽名用的url必須是調用JS接口頁面的完整URL</param> /// <returns></returns> public string GetShal(string jsapi_ticket, string noncestr, long timestamp, string url) { string strSha1 = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}", jsapi_ticket, noncestr, timestamp, url); return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSha1, "sha1").ToLower(); } /// <summary> /// 微信權限簽名( sha1 算法 ) /// 簽名用的noncestr和timestamp必須與wx.config中的nonceStr和timestamp相同 /// </summary> /// <param name="AppId">第三方用戶唯一憑證</param> /// /// <param name="AppSecret">第三方用戶唯一憑證密鑰,即appsecret</param> /// <param name="noncestr">生成簽名的隨機串</param> /// <param name="timestamp">生成簽名的時間戳</param> /// <param name="url">簽名用的url必須是調用JS接口頁面的完整URL</param> /// <returns></returns> public string signature(string AppId, string AppSecret, string noncestr, long timestamp, string url) { string access_token = Getaccess_token(AppId, AppSecret); //獲取全局的access_token string jsapi_ticket = GetJsapi_Ticket(access_token); //獲取jsapi_ticket string strSha1 = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}", jsapi_ticket, noncestr, timestamp, url); return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSha1, "sha1").ToLower(); } /// <summary> /// 微信權限簽名( sha1 算法 ) /// 簽名用的noncestr和timestamp必須與wx.config中的nonceStr和timestamp相同 /// </summary> /// <param name="AppId">第三方用戶唯一憑證</param> /// /// <param name="AppSecret">第三方用戶唯一憑證密鑰,即appsecret</param> /// <param name="noncestr">生成簽名的隨機串</param> /// <param name="timestamp">生成簽名的時間戳</param> /// <param name="url">簽名用的url必須是調用JS接口頁面的完整URL</param> /// <returns></returns> public void signatureOut(string AppId, string AppSecret, string noncestr, long timestamp, string url, out string access_token, out string jsapi_ticket, out string signature) { access_token = Getaccess_token(AppId, AppSecret); //獲取全局的access_token jsapi_ticket = GetJsapi_Ticket(access_token); //獲取jsapi_ticket string strSha1 = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}", jsapi_ticket, noncestr, timestamp, url); signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSha1, "sha1").ToLower(); } private string[] strs = new string[] { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" }; /// <summary> /// 創建隨機字符串 /// </summary> /// <returns></returns> public string CreatenNonce_str() { Random r = new Random(); var sb = new StringBuilder(); var length = strs.Length; for (int i = 0; i < 15; i++) { sb.Append(strs[r.Next(length - 1)]); } return sb.ToString(); } /// <summary> /// 創建時間戳 /// </summary> /// <returns></returns> public long CreatenTimestamp() { return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; } } }
微信 獲取wx.config 參數 基類