• 关于微信授权和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)

  • 相关阅读:
    Android之MessageQueue、Looper、Handler与消息循环
    Eclipse之相关快捷键
    Android之背景颜色小知识(笔记)
    Android之开发常用颜色
    Android之Handler与AsyncTask的区别
    Android之dip、dp、px、sp和屏幕密度
    the setting of serial port in the SecureCRT
    Raspberry Pi 3 Basic Command and Information
    Raspberry Pi 3 --- identify the version of linux kernal file
    Linux C/C++ ------ “” and <> in the use of head include file(Pending Verification)
  • 原文地址:https://www.cnblogs.com/chongyao/p/6278616.html
Copyright © 2020-2023  润新知