您的位置:首页 > 移动开发 > 微信开发

微信JS-SDK中config接口注入权限验证配置

2017-08-10 22:45 881 查看
微信开发平台文档:

wx.config({

    debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。

    appId: '', // 必填,公众号的唯一标识

    timestamp: , // 必填,生成签名的时间戳

    nonceStr: '', // 必填,生成签名的随机串

    signature: '',// 必填,签名,见附录1

    jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2

});
appid参数的获取:
登录微信公众号:“开发”--》“基本配置”--》“公众号开发信息”,如图,就可以获取appid参数了:



timestamp参数的获取:

/// <summary>
/// 将c# DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name="time">时间</param>
/// <returns>long</returns>
private long ConvertDateTimeInt(System.DateTime time)
{

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t
bed3
= (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}


nonceStr参数的获取:随意一个字符串即可;

signature参数的获取:
首先需要需要获得全局的access_token:
private object GetGongZhongHaoTokenParam(string AppId, string AppSecret)
{
System.Web.Caching.Cache objCache = HttpContext.Cache;
object myToken = objCache["access_token"];
if (myToken == null)
{
CookieContainer myCookieContainer = new CookieContainer();
string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppId, AppSecret);
JObject ja = JObject.Parse(GetWebContent(url, ref myCookieContainer));
string token = ja["access_token"].ToString();
int expires = int.Parse(ja["expires_in"].ToString());
objCache.Insert("access_token", token, null, DateTime.Now.AddSeconds(expires - 1000), System.Web.Caching.Cache.NoSlidingExpiration);
myToken = objCache["access_token"];
}
return myToken;
}

private  string GetWebContent(string url, ref CookieContainer myCookieContainer)
{
string curNewUrl = string.Empty;
string html = string.Empty;
HttpWebRequest req = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
req = WebRequest.Create(url) as HttpWebRequest;
req.ProtocolVersion = HttpVersion.Version10;
}
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "Post";
req.AllowAutoRedirect = false;
req.KeepAlive = true;
req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
req.Accept = "text/html,application/xhtml+xml,*/*";
req.CookieContainer = myCookieContainer;
req.ContentType = "application/x-www-form-urlencoded";

using (HttpWebResponse wr = (HttpWebResponse)req.GetResponse())
{
wr.Cookies = req.CookieContainer.GetCookies(req.RequestUri);
curNewUrl = wr.ResponseUri.OriginalString;
myCookieContainer.Add(wr.Cookies);
List<Cookie> cookies = GetAllCookies(myCookieContainer);
myCookieContainer = new CookieContainer();
for (int i = 0; i < cookies.Count; i++)
{
myCookieContainer.Add(cookies[i]);
}
Stream responseStream = wr.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);

html = streamReader.ReadToEnd();

}

return html;
}










得到全局的access_token之后,调用接口:
https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
返回json:

{

"errcode":0,

"errmsg":"ok",

"ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",

"expires_in":7200

}

通过json我们可以得到jsapi_ticket:

object myTicket = null;
//string openid = string.Empty;//openid
if (myTicket == null)
{
CookieContainer myCookieContainer = new CookieContainer();
string url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", myToken.ToString());
JObject ja = JObject.Parse(GetWebContent(url, ref myCookieContainer));
string token = ja["ticket"].ToString();
//openid = ja["openid"].ToString();   //获取openid
objCache.Insert("ticket", token, null, DateTime.Now.AddSeconds(3600), System.Web.Caching.Cache.NoSlidingExpiration);
myTicket = objCache["ticket"];
}


最终通过得到的三个参数获得signature参数:

string stream = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}", (string)myTicket, noncestr(随机字符串), timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分));
//url为:Request.Url.AbsoluteUri
ViewBag.MySignature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(stream, "SHA1");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: