關於微信授權和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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM