예제 #1
0
        public object Login(UserSignIn usr)
        {
            #region CheckIsEmpty
            if (String.IsNullOrWhiteSpace(usr.Email) || String.IsNullOrWhiteSpace(usr.Password))
            {
                return(BadRequest("Email and Password cannot be empty"));
            }
            #endregion

            User user = _context.Users.FirstOrDefault(u => u.Email == usr.Email);

            #region CheckIsNull
            if (user == null)
            {
                return(NotFound());
            }
            #endregion

            #region CheckPasswordIsIncorrect
            if (!Crypto.VerifyHashedPassword(user.Password, usr.Password))
            {
                return(StatusCode(401));
            }
            #endregion

            user.Token = Guid.NewGuid().ToString().Replace("-", string.Empty);
            _context.SaveChanges();

            return(Ok(new
            {
                userId = user.Id,
                token = user.Token
            }));
        }
예제 #2
0
        public object Reset([FromBody] UserSignIn usr)
        {
            //#region CheckIdIsEmpty
            //if(String.IsNullOrWhiteSpace(usr.Id))
            //#endregion

            #region CheckIsNullOrEmpty
            if (string.IsNullOrWhiteSpace(usr.Email) || string.IsNullOrWhiteSpace(usr.NewPassword) || string.IsNullOrWhiteSpace(usr.Password))
            {
                return(StatusCode(402, "Email,Password or New password is null or empty"));
            }
            #endregion

            User user = _context.Users.FirstOrDefault(u => u.Id == usr.Id && u.Email == usr.Email);

            #region CheckIsNull
            if (user == null)
            {
                return(NotFound());
            }
            #endregion

            #region CheckToken
            if (!String.IsNullOrWhiteSpace(Request.Headers["token"]))
            {
                if (user.Token != Request.Headers["token"])
                {
                    return(StatusCode(401));
                }
            }
            else
            {
                return(StatusCode(401));
            }
            #endregion

            #region CheckOldPassword
            if (!Crypto.VerifyHashedPassword(user.Password, usr.Password))
            {
                return(StatusCode(409, "Password incorrect!"));
            }
            #endregion

            #region CheckPasswordIsInvalid
            if (!PasswordValid.Valid(usr.NewPassword))
            {
                ModelState.AddModelError("Password", PasswordValid.Message);
            }
            #endregion

            #region CheckModelIsInvalid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            #endregion

            user.Password = Crypto.HashPassword(usr.NewPassword);

            /*save changes*/
            try
            {
                _context.SaveChanges();
            }
            catch (Exception)
            {
            }

            return(NoContent());
        }