How to get connection string out of Azure KeyVault?

后端 未结 4 2077
花落未央
花落未央 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:43

    What is the actual api?

    We could use the GetSecret API to get value.

    Preparation:

    Registry Azure Active Directory application and assign Role

    Steps:

    1.Create KeyVault and add secret from Azure portal

    2.Config Access policy

    3.Get Access token

     var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
                ClientCredential clientCredential = new ClientCredential(appId, secretKey);
                var tokenResponse =await context.AcquireTokenAsync("https://vault.azure.net", clientCredential);
                var accessToken = tokenResponse.AccessToken;
                return accessToken;
    

    Note: The resource for Keyvault is https://vault.azure.net

    4.Test with Fiddler

    We also can do that easily with SDK:

    1.Create a console project and a Utils.cs file

    public static string EncryptSecret { get; set; }
            static string appId = "Application ID";
            static string secretKey = "Secert key";
            static string tenantId = "TenantId";
    
            public static async Task GetAccessToken(string azureTenantId,string azureAppId,string azureSecretKey)
            {
    
                var context = new AuthenticationContext("https://login.windows.net/" + tenantId);
                ClientCredential clientCredential = new ClientCredential(appId, secretKey);
                var tokenResponse =await context.AcquireTokenAsync("https://vault.azure.net", clientCredential);
                var accessToken = tokenResponse.AccessToken;
                return accessToken;
            }
    

    2.Add the follow code in the main function and test it.

    packages.config file

    
    
      
      
      
      
      
      
      
      
      
      
    
    

    We also can get more information from CtrlDot mentioned document.

提交回复
热议问题