/// <summary> /// 获取OpenId /// </summary> public static string GetOpenID(string appid, string redirect_url, string code, string screct) { string strJson = ""; if (string.IsNullOrEmpty(code)) { redirect_url = HttpUtility.UrlEncode(redirect_url); LogApp.Log4Net.Info("redirect_url:" + redirect_url); LogApp.Log4Net.Info("weixin:" + string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect", appid, redirect_url, new Random().Next(1000, 200000).ToString())); HttpContext.Current.Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state={2}#wechat_redirect", appid, redirect_url, new Random().Next(1000, 200000).ToString())); } else { strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, screct, code)); } return(Tools.GetJsonValue(strJson, "openid")); }
/// <summary> /// 获取OpenId /// </summary> public static string GetOpenID(string appid, string redirect_url, string code, string screct) { string strJson = ""; if (string.IsNullOrEmpty(code)) { redirect_url = HttpUtility.UrlEncode(redirect_url); HttpContext.Current.Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect", appid, redirect_url, new Random().Next(1000, 200000).ToString())); int sTimeOutHours = UserConsts.SessionTimeOutHours.ConfigValue().ToInt32(); sTimeOutHours = 30 * 24; new CookieHelper().Set("openid", Tools.GetJsonValue(strJson, "openid"), sTimeOutHours); } else { strJson = HttpRequestUtil.RequestUrl(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, screct, code)); } return(Tools.GetJsonValue(strJson, "openid")); }