/// <summary> /// singature签名的生成比较麻烦。 /// 首先生成获取access_token /// (有效期7200秒,开发者必须在自己的服务全局缓存access_token) /// </summary> /// <returns></returns> public static string Getaccesstoken() { string appid = PayConfig.WxAppid(); string secret = PayConfig.WxAppSecret(); string urljson = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; string strjson = ""; UTF8Encoding encoding = new UTF8Encoding(); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urljson); myRequest.Method = "GET"; myRequest.ContentType = "application/x-www-form-urlencoded"; HttpWebResponse response; Stream responseStream; StreamReader reader; string srcString; response = myRequest.GetResponse() as HttpWebResponse; responseStream = response.GetResponseStream(); reader = new System.IO.StreamReader(responseStream, Encoding.UTF8); srcString = reader.ReadToEnd(); reader.Close(); if (srcString.Contains("access_token")) { //CommonJsonModel model = new CommonJsonModel(srcString); CommonJsonModel model = new CommonJsonModel(srcString); strjson = model.GetValue("access_token"); SessionTools.SetSession("access_tokenzj", strjson); } return(strjson); }
private static Sys_User Registered(string code) { try { var client = new System.Net.WebClient(); client.Encoding = System.Text.Encoding.UTF8; // 通过code换取网页授权access_token var strTokenUrl = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", PayConfig.WxAppid(), PayConfig.WxAppSecret(), code); var strTokenData = client.DownloadString(strTokenUrl); var TokenEntity = JsonHelper.DeserializeObject <WxAccessToken>(strTokenData); if (TokenEntity == null || string.IsNullOrEmpty(TokenEntity.access_token)) { return(null); } // 通过access_token和openid 拉取用户信息 var strUserUrl = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", TokenEntity.access_token, TokenEntity.openid); var strUserData = client.DownloadString(strUserUrl); var UserEntity = JsonHelper.DeserializeObject <WxUserInfo>(strUserData); if (UserEntity == null || string.IsNullOrEmpty(UserEntity.openid)) { return(null); } // 注册 var User = new UserService().Regist(UserEntity); if (User != null) { SessionTools.UserID = User.UserID; SessionTools.UserName = User.UserName; return(User); } } catch (Exception ex) { throw ex; } return(null); }
private static string GetReturnPath(ActionExecutingContext filterContext, string returnURL) { try { // Session.UserID 为空,则拉取用户注册信息 if (string.IsNullOrEmpty(SessionTools.UserID)) { var code = filterContext.HttpContext.Request.QueryString["code"]; if (string.IsNullOrEmpty(code)) { // CODE 为空,则根据appid拉取网页授权 var redirect_uri = HttpUtility.UrlEncode(PayConfig.WebSiteDomain() + "/Home/OAuth?ReturnUrl=" + returnURL); string url = string.Format(@"https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", PayConfig.WxAppid(), redirect_uri); filterContext.HttpContext.Response.Redirect(url); filterContext.HttpContext.Response.End(); filterContext.Result = new EmptyResult(); return(string.Empty); } // CODE 不为空,则根据 code获取用户信息,并注册 var User = Registered(code); if (User == null) { // 如果User为空,则注册失败,因此不应该继续执行Action内的逻辑 filterContext.HttpContext.Response.End(); filterContext.Result = new EmptyResult(); return(string.Empty); } //// 用户手机号不存在则跳转至完善注册信息界面 //if (User != null && string.IsNullOrEmpty(User.TelePhone)) // return "/Regist/RegistView"; //return "/Regist/RegistView?jsonUser="******"拉取用户网页授权信息并且注册异常", ex.ToString()); } return(string.Empty); }