private void processBind(AuthConnect connect, String code) { if (ctx.viewer.IsLogin == false) { echoError("对不起,请先登录"); return; } // 检查是否绑定过 if (connectService.HasBind(ctx.viewer.Id, connect.GetType().FullName)) { echoError("对不起,已经绑定"); return; } // 获取用户 uid AccessToken x = OAuthClient.New().GetAccessToken(connect, code, connect.HttpMethod_AccessToken); x.Uid = connect.GetUid(x); // 获取用户名称 OAuthUserProfile userProfile = null; try { userProfile = connect.GetUserProfile(x); } catch (HttpClientException ex) { if (ex.Message.IndexOf("applications over the unaudited use restrictions") > 0) { echo(getTestRestrictionsMsg("绑定")); return; } else { throw ex; } } x.Name = userProfile.Name; Result result = connectService.Create(ctx.viewer.obj as User, connect.GetType().FullName, x); // 日志 logger.Info("accessToken=" + x.Token); logger.Info("uid=" + x.Uid); logger.Info("refresh_token=" + x.RefreshToken); logger.Info("expires_in=" + x.ExpiresIn); logger.Info("scope=" + x.Scope); if (result.HasErrors) { echo(result.ErrorsHtml); } else { echoRedirect(lang("opok"), "/"); } }
private void loadUserProfile(AuthConnect connect, AccessToken accessToken) { OAuthUserProfile user = connect.GetUserProfile(accessToken); ctx.SetItem("__currentOAuthUser", user); ctx.SetItem("__currentAccessToken", accessToken); content(loadHtml(confirmUserInfo)); }
public virtual void SaveFirstLogin() { if (ctx.viewer.IsLogin) { echoError("对不起,您已经登录"); return; } Object connectType = ctx.web.SessionGet("__connectType"); if (connectType == null) { echoError("无效的 connect type"); return; } AuthConnect connect = AuthConnectFactory.GetConnect(connectType.ToString()); if (connect == null) { echoError("此连接类型不存在:" + connectType); return; } AccessToken accessToken = getAccessToken(); OAuthUserProfile userProfile = connect.GetUserProfile(accessToken); if (userProfile == null) { echoError("无法获取正常 user profile"); return; } accessToken.Name = userProfile.Name; // 注册用户 User user = new User(); user.Name = ctx.Post("userName"); user.Url = ctx.Post("userUrl"); Result result = userService.RegisterNoPwd(user); if (result.HasErrors) { echoError(result); return; } result = AvatarUploader.SaveRemote(userProfile.PicUrlBig, user.Id); if (result.IsValid) { user.Pic = result.Info.ToString(); user.update(); } else { echoError(result); return; } // 是否开启空间 RegHelper.CheckUserSpace(user, ctx); // 绑定用户 Result saveResult = connectService.Create(user, connect.GetType().FullName, accessToken); if (saveResult.IsValid) { UserConnect userConnect = saveResult.Info as UserConnect; loginService.Login(user, userConnect.Id, LoginTime.OneWeek, ctx.Ip, ctx); // 登录 echoRedirect("登录成功", "/"); } else { echoError(saveResult); } }