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

posted @ 2017-01-12 15:59  雨V幕  阅读(6867)  评论(0编辑  收藏  举报