微信自定义分享:invalid signature签名错误!求解?

坠落的流星-tony 2015-12-26 03:17:07
官方提供的排错方法:
invalid signature签名错误。建议按如下顺序检查:
1)、确认签名算法正确,可用 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 页面工具进行校验。
2)、确认config中nonceStr(js中驼峰标准大写S), timestamp与用以签名中的对应noncestr, timestamp一致。
3)、确认url是页面完整的url(请在当前页面alert(location.href.split('#')[0])确认),包括'http(s)://'部分,以及'?'后面的GET参数部分,但不包括'#'hash后面的部分。
4)、确认 config 中的 appid 与用来获取 jsapi_ticket 的 appid 一致。
5)、确保一定缓存access_token和jsapi_ticket。
6)、确保你获取用来签名的url是动态获取的,动态页面可参见实例代码中php的实现方式。如果是html的静态页面在前端通过ajax将url传到后台签名,前端需要用js获取当前页面除去'#'hash部分的链接(可用location.href.split('#')[0]获取,而且需要encodeURIComponent),因为页面一旦分享,微信客户端会在你的链接末尾加入其它参数,如果不是动态获取当前链接,将导致分享后的页面签名失败。

其中使用(1)校验工具,确认生成的签名是正确,排(2)、(3)(写死的调用 jweixin.0.0.0.js 页面地址)、(4)(直接通过接口生成签名返回的 json 给的配置文件)、(5)(生成后保存在 session 里)、(6)没看懂!

/// <summary>
/// 获取微信凭证 signature
/// </summary>
protected void GetAccessSignature()
{
string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string r = string.Empty;
Random random = new Random();
Enumerable.Repeat<int>(0, 16).ToList().ForEach(x => r += s[random.Next(s.Length)]);
string noncestr = r;
string jsapi_ticket = GetAccessTicket();
int timestamp = Int32.Parse(((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString());
string url = "http://wechat.xxxx.com/list.aspx";
string queryString = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url;
string signature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(queryString, "SHA1");

Msignature mC = new Msignature();
mC.appid = ConfigurationManager.AppSettings["Appid"].ToString();
mC.noncestr = noncestr;
mC.timestamp = timestamp;
mC.signature = signature.ToLower();
Response.Write(JsonConvert.SerializeObject(mC));
}


_appid = wC[0].appid;
_timestamp = wC[0].timestamp;
_nonceStr = wC[0].noncestr;
_signature = wC[0].signature;
//alert(_signature);
wx.config({
debug: false,
appId: _appid,
timestamp: _timestamp,
nonceStr: _nonceStr,
signature: _signature,
jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage']
});
//alert(location.href.split('#')[0]);
wx.ready(function () {
wx.onMenuShareTimeline({
title: '测试',
link: 'http://wechat.xxxx.com',
imgUrl: 'http://www.tyninfo.com/data/upload/shop/editor/web-122-122-1.jpg?488',
success: function () {
alert("分享成功");
},
cancel: function () {
alert("您取消了分享");
}
});
...全文
524 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧