微信jssdk .net 版本

微信 .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>



下面 我把 代码源码关键的 wx帮助类源码附上 (写得很烂 ,大家自己整理)

拿来主义直接拿去用吧 需要 .net 3.5以上版本 

这个是 高人封装好的 ,也许你更喜欢下面这个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值