您现在的位置是:首页 > 电脑技术查询 > web开发

微信 js sdk C# 版 invalid signature 有关问题解决

编辑:chaxungu时间:2022-10-10 23:23:55分类:web开发

微信 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剪切出来放在页面上,问题就解决了。