public User Create(User user)
        {
            try
            {
                var UserData = _userRepository.Get(new User {
                    Email = user.Email
                });

                if (UserData != null)
                {
                    throw new Exception("O email informado não está disponível.");
                }

                byte[] passwordHash, passwordSalt;
                PasswordExtension.CreatePasswordHash(user.Password, out passwordHash, out passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;

                _userRepository.Insert(user);

                return(user);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public User PasswordUpdate(User user)
        {
            if (string.IsNullOrEmpty(user.Email) || string.IsNullOrEmpty(user.Password))
            {
                return(null);
            }

            user = _userRepository.Get(new User {
                Email = user.Email
            });

            if (user == null)
            {
                throw new Exception("User not found");
            }

            byte[] passwordHash, passwordSalt;

            PasswordExtension.CreatePasswordHash(user.Password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _userRepository.Update(user);

            return(user);
        }
        public object Authenticate(User user)
        {
            bool ValidCredentials = false;
            User userBase         = null;

            if (user != null && !string.IsNullOrWhiteSpace(user.Email) && !string.IsNullOrWhiteSpace(user.Password))
            {
                IUserRepository userRepository = new UserRepository(_configuration);
                userBase = userRepository.Get(new User {
                    Email = user.Email
                });

                if (userBase == null)
                {
                    throw new Exception("User not found!");
                }

                ValidCredentials = PasswordExtension.VerifyPasswordHash(user.Password, userBase.PasswordHash, userBase.PasswordSalt);
            }

            if (ValidCredentials)
            {
                return(CreateToken(userBase));
            }
            else
            {
                return(new
                {
                    authenticated = false,
                    message = "Authentication failed"
                });
            }
        }
示例#4
0
        public void ValidarSenha(string senha)
        {
            Senha = senha;

            AddNotifications(new Contract <Notification>()
                             .Requires()
                             .IsNotNullOrEmpty(Senha, nameof(Senha), "A senha do agente é obrigatória"));

            Senha = PasswordExtension.EncriptarSenha(senha);
        }