首页 > 代码库 > 微信 js sdk C# 版 invalid signature 问题解决
微信 js sdk C# 版 invalid signature 问题解决
采用Senparc.Weixin SDK
服务器端代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Senparc.Weixin.MP.CommonAPIs; using Senparc.Weixin.MP.Helpers; using Senparc.Weixin.Exceptions; namespace Stock.weixin { public partial class wxShare : System.Web.UI.Page { public string timestamp = string.Empty; public string nonceStr = string.Empty; public string signature = string.Empty; private string appId = "wx9a46bbc1613c1b6b"; private string secret = "1234567890"; protected void Page_Load(object sender, EventArgs e) { string ticket = string.Empty; timestamp = JSSDKHelper.GetTimestamp(); nonceStr = JSSDKHelper.GetNoncestr(); JSSDKHelper jssdkhelper = new JSSDKHelper(); try { ticket = JsApiTicketContainer.TryGetTicket(appId, secret); signature = jssdkhelper.GetSignature(ticket, nonceStr, timestamp, Request.Url.AbsoluteUri.ToString()); } catch (ErrorJsonResultException ex) { Common.GetInfo.WriteLog("errorcode:" + ex.JsonResult.errcode.ToString() + " errmsg:" + ex.JsonResult.errmsg); } } } }
前端页面
<script type="text/javascript"> wx.config({ debug: false, appId: 'wx9a46bbc1613c1b6b', timestamp: '<%= timestamp %>', nonceStr: '<%= nonceStr %>', signature: '<%=signature %>', jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'translateVoice', 'startRecord', 'stopRecord', 'onRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard' ] }); </script>
出现错误后,用官方校验signature工具http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign检验签名算法无误。官方说生成签名的url容易会导致invalid signature,因此url就用Request.Url.AbsoluteUri.ToString()代替,而问题依旧。
上面js代码原来是放在一个js文件中,后来把wx.config剪切出来放在页面上,问题就解决了。
微信 js sdk C# 版 invalid signature 问题解决
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。