Esempio n. 1
0
        public static WebchatJsUserinfo GetUserInfo(string userAgent, string CODE)
        {
            WechatConfig      wechatconfig = AccessTokenService.GetWechatConfig();
            WebchatJsUserinfo userinfo     = new WebchatJsUserinfo();
            string            url          = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatconfig.Appid + "&secret=" + wechatconfig.AppSecret + "&code=" + CODE + "&grant_type=authorization_code";

            HttpWebResponse response = HttpWebResponseUtility.CreateGetHttpResponse(url, null, userAgent, null);

            Stream       stream = response.GetResponseStream();
            StreamReader sr     = new StreamReader(stream);
            string       result = sr.ReadToEnd();

            WechatJsToken token = JsonConvert.DeserializeObject <WechatJsToken>(result);


            string url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token.access_token + "&openid=" + token.openid + "&lang=zh_CN";

            HttpWebResponse res     = HttpWebResponseUtility.CreateGetHttpResponse(url2, null, userAgent, null);
            Stream          stream2 = res.GetResponseStream();
            StreamReader    sr2     = new StreamReader(stream2);
            string          result2 = sr2.ReadToEnd();

            userinfo = JsonConvert.DeserializeObject <WebchatJsUserinfo>(result2);

            return(userinfo);
        }
        public static string GetAccessToken()
        {
            string access_token = "";

            WechatConfig wechatConfig = AccessTokenService.GetWechatConfig();

            if (DateTime.Now < DateTime.Parse(wechatConfig.AccessTokenExpiredTime))
            {
                access_token = wechatConfig.AccessToken;
            }
            else
            {
                access_token = AccessTokenService.RefrenshToken(wechatConfig.Appid, wechatConfig.AppSecret);
            }
            return(access_token);
        }