Пример #1
0
        public async Task ChangeProtectionPasswordAsync(string oldPassword, string newPassword)
        {
            try
            {
                if (_protectionBusy)
                {
                    throw new Exception("Data protection is busy.");
                }
                _protectionBusy = true;

                if (!_protectionActivated)
                {
                    throw new Exception("Data protection is not activated");
                }

                var oldDataProtectionEntity = await ReadDataProtectionEntity();

                if (oldDataProtectionEntity == null)
                {
                    throw new Exception("Data protection parameters not found.");
                }

                var oldKey = new DataProtectionKey(oldDataProtectionEntity.Id, oldDataProtectionEntity.Params);

                if (!oldKey.ValidatePassword(oldPassword))
                {
                    throw new Exception("Incorrect old password");
                }

                using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    // Creating the key for the new password
                    var prms = DataProtectionKey.CreateParams(newPassword);
                    var newDataProtectionEntity = await SaveDataProtectionEntity(prms);

                    var newKey = new DataProtectionKey(newDataProtectionEntity.Id, newDataProtectionEntity.Params);
                    newKey.ValidatePassword(newPassword);

                    await ReencryptDatabase(oldKey, newKey);

                    // Delete old key
                    await DeleteDataProtectionEntity(oldKey.KeyId);

                    // Set new key as a current key
                    _key = newKey;
                    transactionScope.Complete();
                }

                // Set activated if detected the not finished password change operation.
                _protectionActivated = true;
            }
            finally
            {
                _protectionBusy = false;
            }
        }
Пример #2
0
        public async Task EnableProtectionAsync(string password)
        {
            try
            {
                if (_protectionBusy)
                {
                    throw new Exception("Data protection is busy.");
                }
                _protectionBusy = true;

                if (string.IsNullOrWhiteSpace(password))
                {
                    throw new ArgumentNullException(nameof(password));
                }

                if (_protectionEnabled)
                {
                    throw new Exception("Data protection is already enabled.");
                }

                var prms = DataProtectionKey.CreateParams(password);

                var dataProtectionEntity = await SaveDataProtectionEntity(prms);

                _key = new DataProtectionKey(dataProtectionEntity.Id, dataProtectionEntity.Params);
                _key.ValidatePassword(password);

                await EncryptDatabase(_key);

                _protectionEnabled   = true;
                _protectionActivated = true;
            }
            finally
            {
                _protectionBusy = false;
            }
        }