Search code examples
azureasp.net-coreconnection-stringazure-keyvaultappsettings

Settings keys to Azure Key Vault for sub-levels


For user secret management, I use user secrets for the development stage and I want to use Azure key vault for release and staging. I have this configuration

"ConnectionStrings": {
  "DefaultConnection": "MySecretConnectionString"
},
"SmtpSettings": {
  "SmtpMailServer": "smtp.mailserver.somewhere",
  "SmtpPort": "465",
  "SenderLogin": "login",
  "SenderDisplayName": "Site ",
  "SenderPassword": "password",
  "SenderEmail": "site@mailserver.somewhere",
  "SecureSocketSettings": "SslOnConnect"
}

Problem arises when i want to set ConnectionStrings:DefaultConnection or SmtpSettings:SenderPassword in in Azure key vault.

Is there any way to assign and use value to nested properties? For example like I do for user-secrets with

dotnet user-secrets set "SmtpSettings:SenderPassword" "########" --project MyProject

but for Azure key vault

az keyvault secret set --name "SmtpSettings:SenderPassword" --value "######" --vault-name MyProjectVault

: is not allowed, Parameter 'secret_name' must conform to the following pattern: '^[0-9a-zA-Z-]+$'.


Solution

  • You should use -- as it stated in documentation

    Azure Key Vault secret names are limited to alphanumeric characters and dashes. Hierarchical values (configuration sections) use -- (two dashes) as a delimiter, as colons aren't allowed in key vault secret names. Colons delimit a section from a subkey in ASP.NET Core configuration. The two-dash sequence is replaced with a colon when the secrets are loaded into the app's configuration.

    az keyvault secret set --name "SmtpSettings--SenderPassword" --value "######" --vault-name MyProjectVault