Public platform Development Learning Series (4): only after the shared content is modified and shared can a page be accessed. Public Platform
The sharing window is intended to pop up when you click a button, but no such function is provided. There are always gains, such as how to modify the shared content. These features are provided by the JS-SDK.
Official documentation: http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html
Bind a domain name-introduce js-write js Code
Front-end code:
<Script> wx. config ({debug: false, // enable the debugging mode. The returned values of all called APIs are displayed in the client alert. To view the input parameters, you can open them on the pc, the parameter information is output through log and printed only on the pc end. AppId: '@ ViewData ["AppId"]', // required. timestamp: '@ ViewData ["Timestamp"]', // required, signature generation timestamp nonceStr: '@ ViewData ["NonceStr"]', // required. Signature generation random string signature: '@ ViewData ["Signature"]', // required. jsApiList: ['checkjsapi', 'openlocation', 'getlocation', 'onmenusharetimeline', 'onmenushareappmessage', 'scanqrcode'] // required, list of JS interfaces to be used. For the list of all JS interfaces, see appendix 2. For details, see: http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html }); Wx. error (function (res) {console. log (res); alert ('verification failed') ;}); wx. ready (function () {wx. onMenuShareTimeline ({title: 'custom share title', // share the title link :' http://www.baidu.com ', // Share link imgUrl :' http://images.cnitblog.com/i/340216/201404/301756448922305.jpg ', // Share icon success: function () {$. post ("/Home/GetCookie"); alert ('forwarding successful! ') ;}, Cancel: function () {alert ("failed forwarding") ;}, fail: function (res) {alert (JSON. stringify (res) ;}}) ;}); </script>
The background code for obtaining timestamp and other information is as follows:
public ActionResult Index() { var timestamp = JSSDKHelper.GetTimestamp(); var nonceStr = JSSDKHelper.GetNoncestr(); string ticket = AccessTokenContainer.TryGetJsApiTicket(appId, secret); var signature = JSSDKHelper.GetSignature(ticket, nonceStr, timestamp, Request.Url.AbsoluteUri); ViewData["AppId"] = appId; ViewData["Timestamp"] = timestamp; ViewData["NonceStr"] = nonceStr; ViewData["Signature"] = signature; return View(); }
The following content has nothing to do with development, but it is just a record.
Based on your needs, you need to share it before you can access a page. My idea is to share success again as a post request, return a cookie, and record a special value in the cookie. Similarly, the same value is recorded in the session and the logic judgment is performed on the page. Code:
Public ActionResult ProtectView () {if (Request. Cookies ["protect"]! = Null & Session ["protect"]! = Null) {if (Session ["protect"]. toString () = Request. cookies ["protect"]. value) {return Content ("You have the right to access this page");} return Content ("you do not have the right to access this page. You must first share the text link ");} return Content ("You are not authorized to access this page, you need to share the text link first ");}
The action of the post request is:
[HttpPost] public void GetCookie() { Guid guid = Guid.NewGuid(); HttpCookie cookie = Request.Cookies["protect"]; cookie = new HttpCookie("protect",guid.ToString()); Session["protect"] = guid.ToString(); cookie.Expires = DateTime.Now.AddMinutes(10); Response.Cookies.Add(cookie); }