Ejemplo n.º 1
0
        public AuthenticationResultEnum Authenticate()
        {
            _logger.Info($"Method for authenticating user fired. User name = {_userName}.");

            bool   userNameExists;
            string hashedPassword;

            byte[] salt;

            //obtain login data from DB
            LoginDataFromDBObtainer obtainer = new LoginDataFromDBObtainer(_realmProvider);

            (userNameExists, hashedPassword, salt) = obtainer.GetUserLoginDataFromDB(_userName);

            //if there is no user wuth equal user name
            if (!userNameExists)
            {
                return(AuthenticationResultEnum.UsedDoesNotExist);
            }

            //username exiss, but reading hashed password and salt failed
            if (hashedPassword == null || salt == null)
            {
                return(AuthenticationResultEnum.InternalError);
            }
            else
            {
                string decryptedHashedPassword = null;

                //decrypting password
                PasswordEncryptor decryptor = new PasswordEncryptor();
                try
                {
                    decryptedHashedPassword = decryptor.DecryptPassword(hashedPassword, _password, salt);
                }
                catch (Exception ex)
                {
                    _logger.Error($"Error while decrypting password (not matching): {ex.Message}.");
                }

                //if password is correct
                if (decryptedHashedPassword == _password)
                {
                    _loginSuccessful = true;
                    return(AuthenticationResultEnum.LoginSuccessfull);
                }
                //if password is incorrect
                else
                {
                    return(AuthenticationResultEnum.WrongPassword);
                }
            }
        }
Ejemplo n.º 2
0
        public string Decrypt(string password)
        {
            var encryptor = new PasswordEncryptor(defaultPassword, secret, salt);

            return(encryptor.DecryptPassword(password));
        }