• 关于微信授权和unionid 的获取思路。


    1.首先根据appid 获取到预授权码的code

      string Appid = "******";//appid。由于网页授权与js-jdk使用不同微信,所以暂时独立于此处。
                        string redirect_uri = "http://*********";//这里是回调地址的url
                        string state = filterContext.HttpContext.Request.Url.ToString();//这里的state是
                        state = state.Replace("?", "|").Replace("&", "!!");
                        string url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Appid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=" + state + "#wechat_redirect";
                        filterContext.HttpContext.Response.Redirect(url);

    2.根据预授权码获取个人的用户信息

    //根据code获取预授权码asstoken 和 openid 和Unionid
         string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + df.getAppid() + "&secret=" + df.getAppSecret() + "&code=" + code + "&grant_type=authorization_code"; WebClient wc = new WebClient(); wc.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = wc.DownloadData(url);   string htmlstr = System.Text.Encoding.GetEncoding("utf-8").GetString(pageData);
        
    //根据预授权码获取用户个人信息
    df.get_json方法是获取当前json字符串的当前节点的值
        url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + df.get_json(htmlstr, "access_token") + "&openid=" + df.get_json(htmlstr, "openid") + "&lang=zh_CN";
        wc = new WebClient();
        wc.Credentials = CredentialCache.DefaultCredentials;
        pageData = wc.DownloadData(url);
        htmlstr = System.Text.Encoding.GetEncoding("utf-8").GetString(pageData);
        return htmlstr;

    3.如果想要根据openid 获取Unionid 则需要以下步奏

     3.1根据appid 和appscreat 获取当前公众号的asstoken 

     3.2根据openid和UnionId获取用户信息(这里的用户基本信息只能是关注的才能获取完全基本信息,未关注只能获取openid和unionid)

  • 相关阅读:
    【大数据应用技术】作业一 |了解大数据的特点、来源与数据呈现方式
    结对项目——四则运算 “软件”之升级版
    作业四 个人项目-小学四则运算 “软件”
    作业三:读《构建之法》1-5章有感
    分布式版本控制系统Git的安装与使用
    用热情点燃软件工程II
    字符串、文件操作,英文词频统计预处理
    了解大数据的特点、来源与数据呈现方式
    作业五
    第四次作业
  • 原文地址:https://www.cnblogs.com/chongyao/p/6278616.html
Copyright © 2020-2023  润新知