Exemple #1
0
        /// <summary>登录成功</summary>
        /// <param name="client">OAuth客户端</param>
        /// <param name="service">服务提供者。可用于获取HttpContext成员</param>
        /// <returns></returns>
        public virtual String OnLogin(OAuthClient client, IServiceProvider service)
        {
            var openid = client.OpenID;

            if (openid.IsNullOrEmpty())
            {
                openid = client.UserName;
            }

            // 根据OpenID找到用户绑定信息
            var uc = UserConnect.FindByProviderAndOpenID(client.Name, openid);

            if (uc == null)
            {
                uc = new UserConnect {
                    Provider = client.Name, OpenID = openid
                }
            }
            ;

            uc.Fill(client);

            // 强行绑定,把第三方账号强行绑定到当前已登录账号
            var forceBind = false;
            var req       = service.GetService <HttpRequest>();

            if (req != null)
            {
                forceBind = req["sso_action"].EqualIgnoreCase("bind");
            }

            // 检查绑定
            var user = Provider.FindByID(uc.UserID);

            if (forceBind || user == null || !uc.Enable)
            {
                user = OnBind(uc, client);
            }

            // 填充昵称等数据
            Fill(client, user);

            if (user is IAuthUser user3)
            {
                user3.Save();
            }
            uc.Save();

            if (!user.Enable)
            {
                throw new InvalidOperationException("用户已禁用!");
            }

            // 登录成功,保存当前用户
            Provider.Current = user;

            return(SuccessUrl);
        }
Exemple #2
0
        /// <summary>登录成功</summary>
        /// <param name="client">OAuth客户端</param>
        /// <param name="context">服务提供者。可用于获取HttpContext成员</param>
        /// <param name="uc">用户链接</param>
        /// <returns></returns>
        public virtual String OnLogin(OAuthClient client, IServiceProvider context, UserConnect uc)
        {
            // 强行绑定,把第三方账号强行绑定到当前已登录账号
            var forceBind = false;

#if __CORE__
            var httpContext = context.GetService <IHttpContextAccessor>().HttpContext;
            var req         = httpContext.Request;
            var ip          = httpContext.GetUserHost();
#else
            var req         = context.GetService <HttpRequest>();
            var httpContext = req.RequestContext.HttpContext;
            var ip          = httpContext.GetUserHost();
#endif
            //if (req != null) forceBind = req.Get("sso_action").EqualIgnoreCase("bind");
            if (req != null)
            {
                forceBind = req.Get("state").EndsWithIgnoreCase("_bind");
            }

            // 可能因为初始化顺序的问题,导致前面没能给Provider赋值
            var prv = Provider;
            if (prv == null)
            {
                prv = Provider = ManageProvider.Provider;
            }

            // 检查绑定,新用户的uc.UserID为0
            var user = prv.FindByID(uc.UserID);
            if (forceBind || user == null || !uc.Enable)
            {
                user = OnBind(uc, client);
            }

            // 填充昵称等数据
            Fill(client, user);

            if (user is IAuthUser user3)
            {
                user3.Logins++;
                user3.LastLogin   = DateTime.Now;
                user3.LastLoginIP = ip;
                //user3.Save();
                //(user3 as IEntity).Update();
            }
            if (user is IEntity entity)
            {
                entity.Update();
            }

            try
            {
                uc.UpdateTime = DateTime.Now;
                uc.Save();
            }
            catch (Exception ex)
            {
                //为了防止某些特殊数据导致的无法正常登录,把所有异常记录到日志当中。忽略错误
                XTrace.WriteException(ex);
            }

            // 写日志
            var log = LogProvider.Provider;
            log?.WriteLog(typeof(User), "SSO登录", true, $"[{user}]从[{client.Name}]的[{client.UserName}]登录", user.ID, user + "");

            if (!user.Enable)
            {
                throw new InvalidOperationException($"用户[{user}]已禁用!");
            }

            // 登录成功,保存当前用户
            //prv.Current = user;
            prv.SetCurrent(user, context);
            // 单点登录不要保存Cookie,让它在Session过期时请求认证中心
            //prv.SaveCookie(user);
            var set = Setting.Current;
            if (set.SessionTimeout > 0)
            {
                var expire = TimeSpan.FromSeconds(set.SessionTimeout);
#if __CORE__
                prv.SaveCookie(user, expire, httpContext);
#else
                prv.SaveCookie(user, expire, httpContext.ApplicationInstance.Context);
#endif
            }

            return(SuccessUrl);
        }
Exemple #3
0
        protected override void ShowPage()
        {
            this.pagetitle = "用户控制面板";
            if (!base.IsLogin())
            {
                return;
            }
            UserConnect userConnectInfo = UserConnect.FindByUid(userid);

            if (this.isbindconnect)
            {
                //userConnectInfo = DiscuzCloud.GetUserConnectInfo(this.userid);
                this.isconnectsetpassword = (userConnectInfo != null && !userConnectInfo.IsSetPassword);
            }
            if (DNTRequest.IsPost())
            {
                var    userInfo = Users.GetUserInfo(this.userid);
                string @string  = DNTRequest.GetString("newpassword");
                if (!this.isconnectsetpassword)
                {
                    //if (this.config.Passwordmode > 1 && PasswordModeProvider.GetInstance() != null)
                    //{
                    //    if (!PasswordModeProvider.GetInstance().CheckPassword(userInfo, DNTRequest.GetString("oldpassword")))
                    //    {
                    //        base.AddErrLine("您的原密码错误");
                    //        return;
                    //    }
                    //}
                    //else
                    {
                        if (BBX.Entity.User.Check(this.userid, DNTRequest.GetString("oldpassword"), true) == null)
                        {
                            base.AddErrLine("您的原密码错误");
                            return;
                        }
                    }
                }
                if (@string != DNTRequest.GetString("newpassword2"))
                {
                    base.AddErrLine("新密码两次输入不一致");
                    return;
                }
                if (Utils.StrIsNullOrEmpty(@string))
                {
                    @string = DNTRequest.GetString("oldpassword");
                }
                if (@string.Length < 6)
                {
                    base.AddErrLine("密码不得少于6个字符");
                    return;
                }
                userInfo.Password = @string;
                Users.ResetPassword(userInfo);
                Sync.UpdatePassword(userInfo.Name, userInfo.Password, "");
                if (!Utils.StrIsNullOrEmpty(DNTRequest.GetString("changesecques")))
                {
                    Users.UpdateUserSecques(this.userid, DNTRequest.GetInt("question", 0), DNTRequest.GetString("answer"));
                }
                ForumUtils.WriteCookie("password", ForumUtils.SetCookiePassword(userInfo.Password, this.config.Passwordkey));
                Online.UpdatePassword(this.olid, userInfo.Password);
                if (this.isconnectsetpassword && userConnectInfo.Uid == this.userid)
                {
                    userConnectInfo.IsSetPassword = true;
                    //DiscuzCloud.UpdateUserConnectInfo(userConnectInfo);
                    userConnectInfo.Save();
                }
                base.SetUrl("usercpnewpassword.aspx");
                base.SetMetaRefresh();
                base.SetShowBackLink(true);
                base.AddMsgLine("修改密码完毕, 同时已经更新了您的登录信息");
            }
        }
Exemple #4
0
        /// <summary>登录成功</summary>
        /// <param name="client">OAuth客户端</param>
        /// <param name="context">服务提供者。可用于获取HttpContext成员</param>
        /// <returns></returns>
        public virtual String OnLogin(OAuthClient client, IServiceProvider context)
        {
            var openid = client.OpenID;

            if (openid.IsNullOrEmpty())
            {
                openid = client.UserName;
            }

            // 根据OpenID找到用户绑定信息
            var uc = UserConnect.FindByProviderAndOpenID(client.Name, openid);

            if (uc == null)
            {
                uc = new UserConnect {
                    Provider = client.Name, OpenID = openid
                }
            }
            ;

            uc.Fill(client);

            // 强行绑定,把第三方账号强行绑定到当前已登录账号
            var forceBind = false;
            var req       = context.GetService <HttpRequest>();

            if (req != null)
            {
                forceBind = req.GetRequestValue("sso_action").EqualIgnoreCase("bind");
            }

            // 检查绑定,新用户的uc.UserID为0
            var prv  = Provider;
            var user = prv.FindByID(uc.UserID);

            if (forceBind || user == null || !uc.Enable)
            {
                user = OnBind(uc, client);
            }

            // 填充昵称等数据
            Fill(client, user);

            if (user is IAuthUser user3)
            {
                user3.Logins++;
                user3.LastLogin   = DateTime.Now;
                user3.LastLoginIP = WebHelper.UserHost;
                user3.Save();
            }

            try
            {
                uc.Save();
            }
            catch (Exception ex)
            {
                //为了防止某些特殊数据导致的无法正常登录,把所有异常记录到日志当中。忽略错误
                XTrace.WriteException(ex);
            }

            if (!user.Enable)
            {
                throw new InvalidOperationException("用户已禁用!");
            }

            // 登录成功,保存当前用户
            //prv.Current = user;
            prv.SetCurrent(user, context);
            // 单点登录不要保存Cookie,让它在Session过期时请求认证中心
            //prv.SaveCookie(user);
            var set = Setting.Current;

            if (set.SessionTimeout > 0)
            {
                prv.SaveCookie(user, TimeSpan.FromSeconds(set.SessionTimeout), context);
            }

            LogProvider.Provider.WriteLog(user.GetType(), client.Name, "单点登录", user.ID, user + "", req.GetDisplayUrl());//.UserHostAddress);

            return(SuccessUrl);
        }