Js sdk C # invalid signature Solution
Use Senparc. Weixin SDK
Server code:
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); } } }}
Front-end page
<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>
If an error occurs, use the official signature verification tool www.bkjia.com to verify that the signature algorithm is correct. Officially, the signed url is easy to cause invalid signature. Therefore, the url is replaced by Request. Url. AbsoluteUri. ToString (), and the problem persists.
The above js Code was originally placed in a js file. Later, the wx. config was cut out and placed on the page, and the problem was solved.