How to get connection string out of Azure KeyVault?

后端 未结 4 2078
花落未央
花落未央 2021-01-30 13:42

A hypothetical web-site currently connects using:

public SqlConnection CreateConnection()
{
   DbConnection connection = new SqlConnection();
   connection.Conne         


        
4条回答
  •  [愿得一人]
    2021-01-30 14:36

    For .net Core and already having Azure Key Vault in place. Now it's extremely easy. Let's assume your connection string is in your appsettings.json:

    {
       "ConnectionStrings": {
          "MyDatabase": "server=127.0.0.1;port=5678;database=mydb;user=FullMontyBurns;password=hunter2"
       } 
    }
    

    And you do your setup in Startup.cs:

    string connectionString = configuration.GetConnectionString("MyDatabase");
    

    In Azure Key Vault, create a secret:

    • Name: ConnectionStrings--MyDatabase
    • Value: server=127.0.0.1;port=5678;database=mydb;user=FullMontyBurns;password=hunter2

    The IConfiguration.GetConnectionString("MyDatabase") now takes the value from the Key Vault.

提交回复
热议问题