protected OAuth_token Get_token(String code) { OAuth_token mode = new OAuth_token(); String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code"; HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest; req.Method = "GET"; using (HttpWebResponse res = req.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8); String content = reader.ReadToEnd(); mode = JsonHelper.ParseFromJson <OAuth_token>(content); } return(mode); }
protected UserInfo Get_info(OAuth_token token) { UserInfo mode = new UserInfo(); String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token.access_token + "&openid=" + token.openid + "&lang=zh_CN"; HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest; req.Method = "GET"; using (HttpWebResponse res = req.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8); String content = reader.ReadToEnd(); mode = JsonHelper.ParseFromJson <UserInfo>(content); } return(mode); }
protected void Page_Load(object sender, EventArgs e) { //如果不是POST提交(也就是第一次进入该页面),则初始化页面或控件等等 if (!IsPostBack) { String reUrl = Request.QueryString["reurl"]; String code = Request.QueryString["code"]; OAuth_token token = Get_token(code); UserInfo info = Get_info(token); String redirectUrl = "http://jsu7g5.natappfree.cc/index.aspx"; Session.Add("info", JsonHelper.GetJson <UserInfo>(info)); //Server.Transfer(redirectUrl); Response.Redirect(redirectUrl); } }