微信 .net 版本开发 整体比较简单
验证服务器 获取Access_token 等 都很容易
一下方法可以实现 扫一扫,分享等
让我头疼一点的是 jssdk的地方
我来介绍下我 犯过的错误
首先 获取 timestamp
用下面 这个函数
用法 :ConvertDateTimeInt(DateTime.Now).ToString(); // 时间搓
public int ConvertDateTimeInt(System.DateTime time)
{
int intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (int)(time - startTime).TotalSeconds;
return intResult;
}
再次 noncestr
这个随机取 想怎么搞都可以
我的做法
FormsAuthentication.HashPasswordForStoringInConfigFile(DateTime.Now.toString(), "sha1").ToLower().Substring(0, 16);; //随机字符
获取 jstick 也有个错误
https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi;
type 必须是 jsapi [我写成 wx_card] 这个卡卷的
服务器 阶段完结了 现在是 js代码了
wx.config({
debug: false,
appId: 'xxxxx',
timestamp: xxxx,
nonceStr: 'xxxxx',
signature: 'xxxxxx',
jsApiList: [
'checkJsApi',
'scanQRCode',
'openProductSpecificView',
]
});
我的错误在于
nonceStr S是大写 我是小写
下面是使用案例 (test.cshtml)
@{
Layout = null;
WXHelp w = new WXHelp();
w.Request = Request;
w.Response = Response;
string acc = w.get_Access_token("", "");
string tk = w.getapi_Tick(acc);
string fmt = "jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}";
string Timeeee = w.ConvertDateTimeInt(DateTime.Now).ToString(); // 时间搓
string sss = FormsAuthentication.HashPasswordForStoringInConfigFile(Timeeee, "sha1").ToLower(); //随机字符
string url = Request.Url.ToString();
sss = sss.Substring(0, 16);
fmt = string.Format(fmt, tk, sss, Timeeee, url);
string signature = FormsAuthentication.HashPasswordForStoringInConfigFile(fmt, "sha1").ToLower();
string nt = DateTime.Now.Ticks.ToString();
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
<meta charset="UTF-8">
<title>ad</title>
</head>
<body>
<button class="btn btn_primary" id="scanQRCode0">scanQRCode(微信)</button>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config({
debug: false,
appId: 'wxa45aa45c0a375e1e',
timestamp: @Timeeee,
nonceStr: '@sss',
signature: '@signature',
jsApiList: [
'checkJsApi',
'scanQRCode',
'openProductSpecificView',
]
});
wx.ready(function () {
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
document.querySelector('#scanQRCode0').onclick = function () {
//alert(1);
//alert(location.href.split('#')[0]);
wx.scanQRCode();
};
</script>
</html>
拿来主义直接拿去用吧 需要 .net 3.5以上版本
这个是 高人封装好的 ,也许你更喜欢下面这个