.net 调用微信接口获取微信用户相关信息

js 前端让当前页返回带code参数

注意,第一步调用获取code时,返回路径必须是域名,不可是IP

window.onload = function () {
        LoadApid();
        var code = getUrlParam("code");
        if (code != null) { //存在code后台存储用户信息
            LoadWXInfo();
        }
        else
        {
          GetWXInf();//不存在  则调用微信接口返回code
        }
    };

//生成带返回Code的微信接口地址
function GetWXInf() {
    var str = window.location.protocol + "//" + window.location.host + window.location.pathname;
    var appid = $("#appid").val();

    var Wx = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + encodeURIComponent(str) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    return Wx;
}

//获取appid赋值给前端
function LoadApid() {
    $.ajax({
        type: "post",
        url: "XX/XX/GetWXInfo.ashx",
        async: false,
        dataType: 'json',
        data: { "action": "GetApid" },
        success: function (ret) {
            $("#appid").val(ret.appid);
        }
    });

}

//存储当前微信用户信息
function LoadWXInfo() {
    var Code = getUrlParam("code");
    if (Code != null) {
        $.ajax({
            type: "post",
            url: "XX/XX/GetWXInfo.ashx",
            async: false,
            dataType: 'json',
            data: { "action": "GetWxInfo", "code": Code },
            success: function (ret) {
                if (ret.code != 0) {
                    alert(ret.msg);
                }
            }
        });
    }
}

ashx 获取传输的code参数    调用其他接口 获取用户信息

ashx里面就是直接把code传过去直接调用方法,不细写了

GetWXInfo wx = new GetWXInfo();
WXName = wx.firstLoad(context.Request["code"]); //获取返回的微信昵称,若出错 则返回错误信息、

using Newtonsoft.Json.Linq;
using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.SessionState;


namespace Dal
{
    public class GetWXInfo
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        //公众号信息部分
        public string appid = ConfigurationManager.AppSettings["AppId"];
        public string appsecret = ConfigurationManager.AppSettings["AppSecret"];
        private String getKeyURL &#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值