How can I write to the SecuredSettings of an existing database from code

StackOverflow https://stackoverflow.com/questions/23127524

  •  05-07-2023
  •  | 
  •  

문제

I'm trying to setup PeriodicBackups for an existing database from code. The PeriodicBackups bundle has been enabled in the web.config.

I'm currently using this code:

            using (var session = systemStore.OpenSession())
            {
                var databaseDocument = session.Load<DatabaseDocument>("Raven/Databases/MyDatabase");
                if (databaseDocument != null)
                {
                    databaseDocument.Settings.Add("Raven/AzureStorageAccount", "MyStorageAccount");                        
                    databaseDocument.SecuredSettings.Add("Raven/AzureStorageKey", "abc123");
                    session.SaveChanges();
                }
            }

This results in the following settings for the database "MyDatabase":

{
  "Id": "MyDatabase",
  "Settings": {
    "Raven/DataDir": "~\\Databases\\MyDatabase",
    "Raven/AzureStorageAccount": "MyStorageAccount"
  },
  "SecuredSettings": {
    "Raven/AzureStorageKey": "<data could not be decrypted>"
  },
  "Disabled": false
}

And PeriodicBackups naturally don't work. I guess it's probably because of the AzureStorageKey that can't be decrypted. Am I supposed to encrypt the values I write to SecuredSettings myself? and if so, then how do I do that?

도움이 되었습니까?

해결책

You cannot directly write to the sys docs in this manner when you have secure settings. You need to use the store.DatabaseCommands.Admin.CreateDatabase() method, which knows how to handle this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top