微信 获取wx.config 参数 基类

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}&timestamp={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}&timestamp={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}&timestamp={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 参数 基类

上一篇:解决h5网页微信分享链接不能显示缩略


下一篇:微信小程序 学习笔记