/// <summary> /// 微信授权验证返回 /// </summary> /// <param name="code">微信验证code</param> /// <param name="state">用户原始访问地址</param> /// <returns></returns> public ActionResult CallBack(string code, string state) { ActionResult result = null; bool isSuccessFul = false; var callBackUrl = state; try { if (string.IsNullOrEmpty(code)) { result = Content("您拒绝了授权"); } else { OAuthAccessTokenResult accessTokenResult = null; //通过,用code换取access_token accessTokenResult = OAuthApi.GetAccessToken(WeChatClient.App.AppId, WeChatClient.App.AppSecret, code); //LogService.LogWexin("AccessToken请求状态", accessTokenResult.errcode.ToString()); if (accessTokenResult.errcode == ReturnCode.请求成功) { //更新用户Open id Cookie Response.Cookies.Add(new HttpCookie(WeChatOpenIdCookieName, accessTokenResult.openid)); //因为第一步选择的是OAuthScope.snsapi_userinfo,这里可以进一步获取用户详细信息 UserService userService = new UserService(); OAuthUserInfo userInfo = OAuthApi.GetUserInfo(accessTokenResult.access_token, accessTokenResult.openid); WeChatUser wechatUser = userService.GetWeChatUser(accessTokenResult.openid); if (wechatUser == null) { userService.WeChatUserSubscribe(accessTokenResult.openid, false, true); wechatUser = userService.GetWeChatUser(accessTokenResult.openid); } else { wechatUser.HasAuthorized = true; } if (wechatUser != null) { // Updates wechat user info wechatUser.AccessToken = accessTokenResult.access_token; wechatUser.AccessTokenExpired = false; wechatUser.AccessTokenExpireTime = DateTime.Now.AddSeconds(accessTokenResult.expires_in); wechatUser.City = userInfo.city; wechatUser.County = userInfo.country; wechatUser.NickName = userInfo.nickname; wechatUser.Photo = userInfo.headimgurl; wechatUser.Province = userInfo.province; wechatUser.Sex = userInfo.sex; if (userService.UpdateWeChatUser(wechatUser)) { //Updates current wechat user Session[WeChatUserInfo] = wechatUser; //LogService.LogWexin("保存微信用户资料成功", ""); isSuccessFul = true; } else { LogService.LogWexin("保存微信用户资料失败", wechatUser.OpenId); } } } } } catch (Exception ex) { LogService.LogWexin("QAuth获取用户信息失败", ex.ToString()); isSuccessFul = false; } if (isSuccessFul) { if (string.IsNullOrWhiteSpace(callBackUrl)) { callBackUrl = "/wechat/user/index"; } // 跳转到用户一开始要进入的页面 result = Redirect(callBackUrl); } else { // 授权失败,重新发发起用户授权 var redirectUrl = GetUrl("/wechat/QAuthCallBack/CallBack"); var appId = WeChatClient.App.AppId; var authUrl = OAuthApi.GetAuthorizeUrl(appId, redirectUrl, callBackUrl, OAuthScope.snsapi_userinfo); result = Redirect(authUrl); } return result; }
/// <summary> /// 处理微信授权结果,更新用户信息 /// </summary> /// <param name="filterContext"></param> private bool HandleWechatAuthResult(string code) { bool isSuccessful = true; try { OAuthAccessTokenResult accessTokenResult = null; //通过,用code换取access_token accessTokenResult = OAuthApi.GetAccessToken(WeChatClient.App.AppId, WeChatClient.App.AppSecret, code); //LogService.LogWexin("AccessToken请求状态", accessTokenResult.errcode.ToString()); if (accessTokenResult.errcode == ReturnCode.请求成功) { //更新用户Open id Cookie var openIdCookie = new HttpCookie(WeChatOpenIdCookieName, accessTokenResult.openid); openIdCookie.Expires = DateTime.Now.AddMonths(1); Response.Cookies.Add(openIdCookie); //因为第一步选择的是OAuthScope.snsapi_userinfo,这里可以进一步获取用户详细信息 UserService userService = new UserService(); OAuthUserInfo userInfo = OAuthApi.GetUserInfo(accessTokenResult.access_token, accessTokenResult.openid); WeChatUser wechatUser = userService.GetWeChatUser(accessTokenResult.openid); // 用户第一次访问,添加用户信息 if (wechatUser == null) { userService.WeChatUserSubscribe(accessTokenResult.openid, false, true); wechatUser = userService.GetWeChatUser(accessTokenResult.openid); } else { wechatUser.HasAuthorized = true; } // 更新用户详细信息 if (wechatUser != null) { // Updates wechat user info wechatUser.AccessToken = accessTokenResult.access_token; wechatUser.AccessTokenExpired = false; wechatUser.AccessTokenExpireTime = DateTime.Now.AddSeconds(accessTokenResult.expires_in); wechatUser.City = userInfo.city; wechatUser.County = userInfo.country; wechatUser.NickName = userInfo.nickname; wechatUser.Photo = userInfo.headimgurl; wechatUser.Province = userInfo.province; wechatUser.Sex = userInfo.sex; if (userService.UpdateWeChatUser(wechatUser)) { //Updates current wechat user Session[WeChatUserInfo] = wechatUser; //LogService.LogWexin("保存微信用户资料成功", ""); } else { LogService.LogWexin("保存微信用户资料失败", wechatUser.OpenId); isSuccessful = false; } } } } catch (Exception ex) { LogService.LogWexin("QAuth获取用户信息发生异常", ex.ToString()); isSuccessful = false; } return isSuccessful; }