.NET微信网页开发之JS-SDK使用步骤和配置信息timestamp(时间戳)(2)
发布时间:2021-01-24 11:56 所属栏目:12 来源:网络整理
导读:? 获取jsapi_ticket微信公众号调用微信JS接口的临时票据(前提是:获取到了公众号全局唯一接口调用凭据): 获取jsapi_ticket微信公众号调用微信JS接口的临时票据 param name="accessToken"微信公众号的全局唯一接
? 获取jsapi_ticket微信公众号调用微信JS接口的临时票据(前提是:获取到了公众号全局唯一接口调用凭据):获取jsapi_ticket微信公众号调用微信JS接口的临时票据 <param name="accessToken">微信公众号的全局唯一接口调用凭证</param> string RequestJsapi_ticket( accessToken) { string content = ; tryTODO:注意api_ticket 是用于调用微信卡券JS API的临时票据,有效期为7200 秒,通过access_token 来获取。 string url = https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + &type=jsapi; ; HttpWebRequest request = WebRequest.Create(url) HttpWebRequest; CookieContainer cookieContainer = CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = ; request.Method = method; request.ContentType = ; request.Headers.Add(); 发送请求并获取响应数据 HttpWebResponse response = request.GetResponse() HttpWebResponse; Stream responseStream = response.GetResponseStream(); StreamReader sr = 获取返回过来的结果 content = sr.ReadToEnd(); dynamic resultStr = JsonConvert.DeserializeObject(content,1)">new { errcode=}.GetType()); 请求成功 if (resultStr.errcode==0"&&resultStr.errmsg==ok) { content=resultStr.ticket; } { content = ; } return content; } catch (Exception ex) { content = ex.Message; content; } } 生成签名的随机串(nonceStr):/// 随机字符串数组集合 </summary> private readonly string[] NonceStrings = new [] { abcdefghijklmnopqrstuvwxyz,ABCDEFGHIJKLMNOPQRSTUVWXYZ }; 生成签名的随机串 CreateNonceStr() { Random random = Random(); var sb = StringBuilder(); var length = NonceStrings.Length; 生成15位数的随机字符串,当然也可以通过控制对应字符串大小生成,但是至多不超过32位 for (int i = 0; i < 15; i++) { sb.Append(NonceStrings[random.Next(length - 1)]);通过random获得的随机索引到,NonceStrings数组中获取对应数组值 sb.ToString(); } 生成签名时间戳(timestamp):获取当前时间戳 long GetCurrentUinxTime() { DateTime currentDate = DateTime.Now;当前时间 转化为时间戳 DateTime localTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1)">1,1)">1)); return long.Parse((currentDate - localTime).TotalSeconds.ToString().Split('.')[0]); } 获取当前网页URL:注意:一定要是在安全域名内,否则生成的是无效的签名。 //获取当前网页URL string currentWebUrl = Request.Url.ToString(); 生成签名(signature):微信JS-SDK使用权限签名算法详细概述:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 获取签名 <param name="jsapi_ticket">微信公众号调用微信JS临时票据<param name="nonceStr">随机串<param name="timestamp">时间戳<param name="url">当前网页URLstring GetSignature(string jsapi_ticket,1)">string nonceStr,1)">long timestamp,1)"> url) { var string1Builder = 注意这里参数名必须全部小写,且必须有序 string1Builder.Append(jsapi_ticket=").Append(jsapi_ticket).Append(&) .Append(noncestr=").Append(nonceStr).Append(timestamp=").Append(timestamp).Append(url=").Append(url.IndexOf(#") >= 0 ? url.Substring(0,url.IndexOf()) : url); Sha1(string1Builder.ToString(),Encoding.UTF8); } 签名加密,使用SHA加密所得 <param name="content">签名加密参数<param name="encode">编码UTF-8string Sha1( content,Encoding encode) { { SHA1 sha1 = SHA1CryptoServiceProvider(); byte[] bytesIn = encode.GetBytes(content); byte[] bytesOut = sha1.ComputeHash(bytesIn); sha1.Dispose(); string result = BitConverter.ToString(bytesOut); result = result.Replace(- result; } (Exception ex) { throw new Exception(SHA1加密出错: ex.Message); } } 总结:(编辑:ASP站长网) |
相关内容
网友评论
推荐文章
热点阅读