WeChat js sdk C # invalid signature Solution

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.