public static GetOpenIdResponse GetOpenidInfoFromCode(string code) { try { SortedDictionary <string, object> dic = new SortedDictionary <string, object>(); dic["appid"] = AppSettingHelper.AppId; dic["secret"] = AppSettingHelper.SecretKey; dic["code"] = code; dic["grant_type"] = "authorization_code"; string tempurl = WXHelper.ToUrl(dic); string url = AppSettingHelper.GetAccessTokeFromUrl + tempurl; WCFClient.LoggerService.Info("UrlInfo:" + url); string result = HttpService.Get(url); Container.LogService.Error("GetOpenidInfoFromCode: " + result); GetOpenIdResponse objResponse = PublicResource.JsonHelper.ConvertJsonStringToObject <GetOpenIdResponse>(result); return(objResponse); } catch (Exception ex) { Container.LogService.Error(ex.ToString()); return(null); } }
/// <summary> /// 页面初始化前 /// </summary> protected override void OnPreInit(EventArgs e) { HttpCookie objCookie = HttpContext.Current.GetCookie(Constants.cookieName_UserInfo); if (objCookie != null && objCookie.Values != null && !string.IsNullOrWhiteSpace(objCookie.Values["oi"])) { NameValueCollection nvc = objCookie.Values; _openId = nvc["oi"].DoTrim(); _name = nvc["name"].DoTrim(); _weixinAccount = nvc["wa"].DoTrim(); _platformUserToken = nvc["pfut"].DoTrim(); _platformUserId = nvc["pfui"].DoTrim(); _phoneNumber = nvc["pn"].DoTrim(); } else { WCFClient.LoggerService.Info("获取微信OpenId 开始"); string code = Request["code"]; string state = Request["state"]; if (string.IsNullOrEmpty(code)) { if (string.IsNullOrWhiteSpace(ToUrl)) { return; } string url = WXHelper.GetAuthorizeCodeUrl(true, ToUrl); WCFClient.LoggerService.Info("获取微信OpenId URl" + url); Response.Redirect(url); } else { GetOpenIdResponse ResponseWithOpenId = WXHelper.GetOpenidInfoFromCode(code); WCFClient.LoggerService.Info("获取微信OpenId code" + code); WCFClient.LoggerService.Info("获取微信OpenId state" + state); if (ResponseWithOpenId != null) { try { _openId = ResponseWithOpenId.openid; _name = ResponseWithOpenId.openid; _weixinAccount = string.Empty; _platformUserToken = string.Empty; _platformUserId = string.Empty; _phoneNumber = string.Empty; WCFClient.LoggerService.Info("获取微信OpenId OpenId" + _openId); NameValueCollection nvc = new NameValueCollection(); nvc["oi"] = _openId; nvc["name"] = _name; nvc["wa"] = _weixinAccount; nvc["pfut"] = _platformUserToken; nvc["pfui"] = _platformUserId; nvc["pn"] = _phoneNumber; HttpContext.Current.AddCookie(Constants.cookieName_UserInfo, nvc, null, HttpContext.Current.Request.Url.Host); } catch (Exception ex) { Container.LogService.Error(ex.ToString()); } } } WCFClient.LoggerService.Info("获取微信OpenId 结束"); } }