C#微信环境分享页面给微信好友、朋友圈

1.js引用

  <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>

2.代码示例   

string appid ="微信公众号appId";

string showimg = "图片地址";
string msg = "描述";
string imgurl = "";
#region 设置分享
StringBuilder share = new StringBuilder();
string shareurl = Request.Url.ToString();
string jumpurl = "分享页可带参数" ;
string timespan = DateTime.Now.ToString("hhmmssffff");

 

//获取token
string acctoken =GetToken();
string ticken =GetJsTiket(acctoken);

 

//组织分享操作
share.Append("wx.config({");
share.Append(" debug:false,");//true 成功失败提示信息
share.Append(" appId: '" + appid + "',");
share.Append(" timestamp: '" + timespan + "',");
share.Append(" nonceStr: '" + timespan + "',");
string zzurl = "jsapi_ticket=" + ticken +
"&noncestr=" + timespan + "&timestamp=" + timespan + "&url=" + shareurl.Trim();
string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(zzurl, "SHA1").ToLower();

share.Append(" signature: '" + sign + "',");
share.Append(" jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage']");
share.Append(" });"); 

//分享到好友
share.Append(" wx.onMenuShareAppMessage({");
share.Append(" title: '标题',");
share.Append(" desc:'" + msg + "',");
share.Append(" link: '" + jumpurl + "', ");
share.Append(" imgUrl: '" + showimg + "', ");
share.Append(" success: function () { },");
share.Append(" cancel: function () {}");
share.Append(" });"); 

//分享到朋友圈
share.Append("wx.ready(function () {");
share.Append(" wx.onMenuShareTimeline({");
share.Append(" title:'标题',");
//share.Append(" desc:'自定义描述',");
share.Append(" link: '" + jumpurl  + "', ");
share.Append(" imgUrl: '" + showimg + "', ");
share.Append(" success: function (res) { },");//alert(JSON.stringify(res));
share.Append(" cancel: function (res) { }");
share.Append(" });");

Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "initkey", share.ToString(), true); 

#endregion 

3.注意事项  

  出现 config: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。 

 如果以上都没有问题 

        看一下微信公众号中:网页授权中是否配置可访问域名 

        注意事项:微信访问页面会有缓存,增加缓存处理

 

原文地址:https://www.cnblogs.com/dsjbk/p/14705294.html