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