Exemplo n.º 1
0
        public IActionResult Signup([FromBody] tblUser model)
        {
            try
            {
                model.Email    = model.Email.Trim().ToLower();
                model.Password = model.Password.Trim();
                string  password = model.Password;
                tblUser _tblUser = _ItblUserRepository.Get(x => x.Email.ToLower() == model.Email).FirstOrDefault();
                if (_tblUser != null)
                {
                    return(BadRequest("Email already exists"));
                }
                else
                {
                    model.Password    = EncryptUtil.EncryptString(model.Password);
                    model.IsActive    = true;
                    model.CreatedDate = DateTime.Now;
                    _ItblUserRepository.Add(model);
                    model.Password = password;
                }

                return(Ok(model));
            }
            catch (Exception ex)
            {
                log.Fatal("signup:", ex);
                return(BadRequest(ex));
            }
        }
Exemplo n.º 2
0
        public LoginModel loginUser(LoginModel model)
        {
            LoginModel _LoginModel = new LoginModel();

            try
            {
                List <tblUserRole> _tblUserRole = new List <tblUserRole>();
                bool    FirstLog = false;
                tblUser _tblUser = new tblUser();

                tblUserLog _tblUserLog = new tblUserLog();
                model.Username = model.Username.Trim();
                model.Password = model.Password.Trim();
                model.Password = EncryptUtil.EncryptString(model.Password);


                model.Username = model.Username.ToLower();



                _tblUser = _ItblUserRepository.Get(x => x.Email.ToLower() == model.Username && x.Password == model.Password && x.IsActive == true && x.IsBlocked == false).FirstOrDefault();


                if (_tblUser != null)
                {
                    _tblUser.tblUserRole    = null;
                    _tblUser.InvalidAttempt = 0;
                    _ItblUserRepository.Update(_tblUser);
                    List <tblRole> _tblUserRole2 = _ItblUserRoleRepository.Get(x => x.FKUser == _tblUser.PKUser && x.IsActive == true).Select(s =>
                                                                                                                                              s.tblRole
                                                                                                                                              ).ToList();

                    foreach (var data in _tblUserRole2)
                    {
                        tblUserRole temp = new tblUserRole();
                        temp.tblRole = data;
                        temp.FKRole  = data.PKRole;

                        _tblUserRole.Add(temp);
                    }


                    _tblUserLog.LoginSuccess = true;
                    _tblUserLog.FKUser       = _tblUser.PKUser;

                    lastLogin = _ItblUserLogRepository.Get(x => x.UserName == _tblUser.Email && x.LoginSuccess == true).ToList();
                    if (lastLogin != null && lastLogin.Count > 0 && _tblUser.IsChangePassword == true)
                    {
                        FirstLog = true;
                    }
                    else
                    {
                        FirstLog = false;
                    }
                }

                else
                {
                    _tblUser = _ItblUserRepository.Get(x => x.Email.ToLower() == model.Username && x.Password == model.Password && x.IsActive == true && x.IsBlocked == true).FirstOrDefault();


                    if (_tblUser != null)
                    {
                        _LoginModel.ErrorMessage = "Your account has been blocked. Please contact to the administrator!";
                    }
                    else
                    {
                        _tblUser = _ItblUserRepository.Get(x => x.Email.ToLower() == model.Username).FirstOrDefault();


                        if (_tblUser != null)
                        {
                            _tblUser.tblUserRole    = null;
                            _tblUserLog.FKUser      = _tblUser.PKUser;
                            _tblUser.InvalidAttempt = _tblUser.InvalidAttempt + 1;
                            _ItblUserRepository.Update(_tblUser);
                        }
                        _tblUserLog.LoginSuccess = false;
                        _LoginModel.ErrorMessage = "Invalid Username Or Password";
                    }
                }


                _tblUserLog.UserName  = model.Username;
                _tblUserLog.Password  = model.Password;
                _tblUserLog.LoginTime = DateTime.Now;
                _tblUserLog.IPAddress = context.HttpContext.Connection.RemoteIpAddress.ToString();
                _tblUserLog.Origin    = model.Origin;
                _tblUserLog.IsActive  = true;
                _ItblUserLogRepository.Add(_tblUserLog);
                if (_tblUser != null && _tblUser.InvalidAttempt > 3)
                {
                    tblUser _tblUser1 = new tblUser();

                    _tblUser1 = _ItblUserRepository.Get(x => x.Email.ToLower() == model.Username).FirstOrDefault();



                    if (_tblUser1 != null)
                    {
                        _tblUser1.IsBlocked  = true;
                        _tblUser.IsBlocked   = true;
                        _tblUser.tblUserRole = null;
                        _ItblUserRepository.Update(_tblUser1);
                    }
                }

                if (_tblUser != null)
                {
                    _tblUser.IsActive    = FirstLog;
                    _tblUser.tblUserRole = _tblUserRole;
                    if (_tblUserLog.LoginSuccess == false)
                    {
                        _tblUser = null;
                    }
                    else
                    {
                        _tblUser.Password = null;
                    }
                }

                _LoginModel.Users = _tblUser;
                if (_LoginModel.ErrorMessage != null && _LoginModel.ErrorMessage.Length > 0)
                {
                    _LoginModel.Users = null;
                }
                return(_LoginModel);
            }
            catch (Exception ex)
            {
                _LoginModel.ErrorMessage = ex.Message;
                log.Fatal("login:", ex);
                throw ex;
            }
        }