Esempio n. 1
0
        public bool Login(string username, string password)
        {
            //return new UsersRepository().Login(username, new Encryption().HashString(password));

            LoginAttemptsRepository loginAttempts = new LoginAttemptsRepository();

            bool validLogin = new UsersRepository().Login(username, new Encryption().HashString(password));

            if (validLogin == true)
            {
                return(validLogin);
            }
            else
            {
                string userIp = loginAttempts.GetIP();

                if (loginAttempts.DoesUsernameAttemptExist(username) == false)
                {
                    LoginAttempt l = new LoginAttempt
                    {
                        Username = username,
                        Attempt  = 1,
                        Time     = DateTime.Now,
                        Blocked  = false,
                    };
                    loginAttempts.AddUsernameAttempt(l);
                }
                else
                {
                    LoginAttempt oldusername;

                    if (loginAttempts.DoesUsernameAttemptExist(username) == true)
                    {
                        oldusername = loginAttempts.GetUsernameAttempt(username);
                        bool usernameBlockCheck = (oldusername.Attempt >= 2) ? true : false;

                        LoginAttempt updateUsername = new LoginAttempt
                        {
                            Username = username,
                            Attempt  = oldusername.Attempt++,
                            Time     = oldusername.Time,
                            Blocked  = usernameBlockCheck,
                        };
                        loginAttempts.UpdateUsernameAttempt(oldusername, updateUsername);
                    }
                }

                if (loginAttempts.DoesIpAttemptExist(userIp) == false)
                {
                    IpAttempt i = new IpAttempt
                    {
                        Attempt    = 1,
                        Time       = DateTime.Now,
                        Blocked    = false,
                        Ip_Address = userIp
                    };
                    loginAttempts.AddIpAttempt(i);
                }
                else
                {
                    IpAttempt oldIp;

                    if (loginAttempts.DoesIpAttemptExist(userIp) == true)
                    {
                        oldIp = loginAttempts.GetIpAttempt(userIp);
                        bool ipBlockCheck = (oldIp.Attempt >= 2) ? true : false;

                        IpAttempt updateIp = new IpAttempt
                        {
                            Ip_Address = userIp,
                            Attempt    = oldIp.Attempt++,
                            Time       = oldIp.Time,
                            Blocked    = ipBlockCheck,
                        };
                        loginAttempts.UpdateIpAttempt(oldIp, updateIp);
                    }
                }
                return(validLogin);
            }
        }