一,view代码

<script src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jweixin-1.4.0.js"></script>
@*<script src="~/Scripts/jweixin-1.0.0.js"></script>*@
<script>
wx.config({
debug: true,
appId: "@ViewBag.AppId",
timestamp: "@ViewBag.timestamp",
nonceStr: "@ViewBag.nonceStr",
signature: "@ViewBag.signature", jsApiList: [
"checkJsApi",
"onMenuShareAppMessage",
]
});
wx.ready(function () {
wx.checkJsApi({
jsApiList: ['onMenuShareAppMessage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function (res) {
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
}
}); wx.error(function (res) {
//alert(res);
});
wx.onMenuShareAppMessage({
title: '测试', // 分享标题
desc: '测试', // 分享描述
link: 'http://chwmay.51mypc.cn/Home/Index', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: 'https://i.vzan.cc/image/liveimg/jpeg/2018/4/13/185209cf7bdd0050fd4ced84b0a64aded068ef.jpeg', // 分享图标
type: 'link',
dataUrl: '',
success: function () {
alert();
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
})
</script>

二,控制器代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebTestDemo.Code; namespace WebTestDemo.Controllers
{
/// <summary>
/// 微信JSSDK的使用
/// </summary>
public class JssdkController : BaseController
{
public static string AppId = "";
public static string timestamp = "";
public static string nonceStr = "";
public static string signature = ""; public ActionResult Index()
{
timestamp = JssdkHelper.ConvertDateTimeInt(DateTime.Now).ToString(); //时间戳获取当前时间
nonceStr = "Test" + new Random().Next(, ) + "Demo"; //随机字符串没有固定的字符和没有特定的格式
string url = HttpContext.Request.Url.ToString(); // url(当前网页的URL,不包含#及其后面部分)
url = url.Substring(, url.IndexOf('#') == - ? url.Length : url.IndexOf('#'));
signature = JssdkHelper.ReturnSignature(timestamp, nonceStr, url);
ViewBag.AppId = JssdkHelper.appid;
ViewBag.timestamp = timestamp;
ViewBag.nonceStr = nonceStr;
ViewBag.signature = signature;
return View();
}
}
}

三,JSSDK帮助类代码

    public static class JssdkHelper
{ public const string appid = "你的appid";
public const string appsecret = "你的appsecret ";
public const string baseurl = "https://api.weixin.qq.com/";
public const string openurl = "https://open.weixin.qq.com/";
public const string token = "test"; public static int ConvertDateTimeInt(DateTime time)
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(, , ));
return (int)(time - startTime).TotalSeconds;
} /// <summary>
/// 返回签名
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <returns></returns>
public static string ReturnSignature(string timestamp, string nonce, string url)
{
string tmpStr = string.Empty;
string jsapi_ticket = Getjsapi_ticket();
SortedList<string, string> SLString = new SortedList<string, string>(); SLString.Add("jsapi_ticket", jsapi_ticket);
SLString.Add("noncestr", nonce);
SLString.Add("timestamp", timestamp);
SLString.Add("url", url);
foreach (KeyValuePair<string, string> des in SLString) //返回的是KeyValuePair,在学习的时候尽量少用var,起码要知道返回的是什么
{
tmpStr += des.Key + "=" + des.Value + "&";
}
if (tmpStr.Length > )
tmpStr = tmpStr.Substring(, tmpStr.Length - );
return Utils.SHA1Encrypt(tmpStr);
} /// <summary>
/// 获取Token
/// </summary>
/// <returns></returns>
public static string GetToken()
{
string token = CookieHelper.GetCookie(CookieHelper.StrWXTokenCookieName);
if (string.IsNullOrEmpty(token))
{
string url = string.Format("{0}cgi-bin/token?grant_type=client_credential&appid={1}&secret={2}", baseurl, appid, appsecret);
string str = Utils.HttpGet(url);
JObject j = JObject.Parse(str);
token = j.Value<string>("access_token");
} return token;
}
public static string Getjsapi_ticket()
{
string access_token = GetToken();
string url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token);
string backStr = HttpClientHelper.GetResponse(url);
//string backStr = "{ \"errcode\":0,\"errmsg\":\"ok\",\"ticket\":\"kgt8ON7yVITDhtdwci0qeZWDYY9llY5RrKsWxKD--zOUIRYqJ1XwMo305bwZhG22b5hOl-TZ-gZAXCbMMHwvCw\",\"expires_in\":7200}";
string str_ticket = backStr.Split(',')[].Split(':')[];
string Jsapi_ticket = str_ticket.Substring(, str_ticket.Length - );
return Jsapi_ticket;
} }

四,invalid url domain出现后的解决方法

在微信开发文档中有一个这样的解决方法,当前页面所在域名与使用的appid没有绑定,请确认正确填写绑定的域名,仅支持80(http)和443(https)两个端口,因此不需要填写端口号,但这个是什么意思呢?
1,建议在IIS部署的网站是80端口,如果是还出现问题就看2,就是JS接口安全域名这里的配置有问题,js安全域名怎么配置呢?
2,先去掉http,比如我们的域名是www,xxx.com,我们不能配置成http://www,xxx.com/Jssdk/Index,要去掉http://以及后面的文件目录(/Jssdk/Index),写成www,xxx.com即可

微信jssdk配置的问题,使用MVC制作的demo的更多相关文章

  1. 调用微信JS-SDK配置签名

    前后端进行分开开发: 1:后端实现获取 +++接口凭证:access_token (公众号的全局唯一接口调用凭据) ** GET 获取:https://api.weixin.qq.com/cgi-bi ...

  2. web环境中微信JS-SDK配置

    一.公众号相关设置 首先,在公众号中进行JS安全域名的设置,在公众号设置-功能设置中选择JS接口安全域名,点击设置进入设置对话框.按照要求逐步进行,完成设置. 二.页面请求发送与处理 引入所需js: ...

  3. 前端工程师如何快速的开发一个微信JSSDK应用

    亲们,订阅号出来已经很久了,作为一个前端工程师或者全栈工程师,你是不是错过了什么?大概许多攻城狮同砚还没有反应过来订阅号怎么回事,就马上要被微信的应用号秀一脸了.在应用号还没有正式出来之前,我们赶紧一 ...

  4. 微信jssdk,实现多图上传的一点心得

    一.首先在common.js里封装一个函数,在需要调用jsSDK的页面引用此方法即可实现微信的信息配置function signatureJSSDK() { var url = window.loca ...

  5. 微信JS-SDK分享功能的.Net实现代码

    JS-SDK接口是什么? 为了方便开发者实现微信内的网页(基于微信浏览器访问的网页)功能,比如拍照.选图.语音.位置等手机系统的能力,并方便开发者直接使用微信分享.扫一扫等微信特有的能力,微信推出了J ...

  6. ASP.NET MVC做的微信WEBAPP中调用微信JSSDK扫一扫

    今天做一个项目,是在微信上用的,微信WEB APP,里面用到了调用手机摄像头扫一扫二维码的功能,记得以前某个项目里写有的,但是找不到之前那个项目源码了,想复制粘贴也复制不了了,只好对着微信的那个开发文 ...

  7. ASP.NET MVC 微信JS-SDK认证

    layout: post title: ASP.NET MVC 微信JS-SDK认证 category: .net date: 2016-11-01 00:00:00 tags: .net javas ...

  8. 【原创】.Net 微信 JS-SDK图片、语音上传接口的实现(MVC)-(一 、上传图片)

    前段时间在做一个微信的项目,遇到了一个上传图片的问题,花了一下午,解决了这个问题,然后把总结出来的代码,分享了出来. 最近又有一个图片+语音的功能, 更是蛋疼, 本次采用的不是File文件上传,然后转 ...

  9. C#微信开发-微信JS-SDK(1)之通过config接口注入权限验证配置

    官方文档是微信JS-SDK的使用步骤http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#JSSDK.E4.BD.B ...

随机推荐

  1. 【leetcode】1171. Remove Zero Sum Consecutive Nodes from Linked List

    题目如下: Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum ...

  2. mysql INNER JOIN关键字 语法

    mysql INNER JOIN关键字 语法 作用:在表中存在至少一个匹配时,INNER JOIN 关键字返回行.大理石平台维修 语法:SELECT column_name(s) FROM table ...

  3. .Net手动实现ORM及代码生自动成器

    序言 代码生成器 同时提供便捷的开发管理功能和多项开发工作中常用到的辅助工具功能,您可以很方便轻松地进行项目开发,让软件开发变得轻松而快乐!帮您快速开发项目,缩短开发周期,减少开发成本,大大提高了企业 ...

  4. [JSOI2008]最大数 题解

    前言 巨佬说:要有线段树,于是蒟蒻就开始打线段树. 蒟蒻只能拿之前0分的板子题开刀了QAQ. 题解 一开始我以为插入操作不带取模,于是打了下面这个弱智玩意 下面的代码是会WA的 #include &l ...

  5. [ethereum源码分析](2) ethereum基础知识

    前言 上一章我们介绍了如何搭建ethereum的debug环境.为了更深入的了解ethereum,我们需要了解一些ethereum的相关的知识,本章我们将介绍这些知识. ethereum相关知识 在学 ...

  6. 170815-关于Session知识点

    Cookie:实际上就是一个头.               服务器会创建Cookie,并且将Cookie以一个响应头的形式发送给浏览器               浏览器收到Cookie以后,会保存 ...

  7. [CSP-S模拟测试]:序列(构造)

    题目描述 给定$N,A,B$,构造一个长度为$N$的排列,使得:$\bullet$排列长度为$N$:$\bullet$最长上升子序列长度为$A$:$\bullet$最长下降子序列长度为$B$.我们有$ ...

  8. shell scripts 编写基础

    一.shell变量的相关用法: 变量作为被赋值的一方的时候不加$,只有在使用其值的内容的时候需要加上$,该符号可 1,变量中的单引号‘’.双引号“”“.反单引号‵`.括号().大括号{}.双括号(() ...

  9. 基于python实现自动化办公学习笔记一

    1.CSV(1)写csv文件 import csv def writecsv(path,data): with open(path, "w") as f: writer = csv ...

  10. release,debug库互调用,32位,64位程序与库互调用

    以下是基于visual studio 2015和cmake的实验 1,debug或release的应用程序都可以调用release的库2,win32和x64的应用和库无法互调用,在VS中链接时会有一堆 ...