整合 Azure 應用程式組態 與 Service 連線 or

此頁面會顯示支持的驗證方法和用戶端,並顯示您可以使用Service 連線 or 將 Azure 應用程式組態 連線到其他雲端服務的範例程式代碼。 您仍然可以使用其他方法來連線到 應用程式組態。 此頁面也會顯示您在建立服務連線時取得的預設環境變數名稱和值。

支援的計算服務

服務 連線 器可用來將下列計算服務連線至 Azure 應用程式組態:

  • Azure App Service
  • Azure Functions
  • Azure 容器應用程式
  • Azure Spring Apps

支援的驗證類型和客戶端類型

下表顯示使用 Service 連線 or 將計算服務連線到 Azure 應用程式組態 支援的驗證方法和客戶端組合。 「是」表示支持組合,而「否」則表示不支援。

用戶端類型 系統指派的受控識別 使用者指派的受控識別 秘密/連接字串 服務主體
.NET Yes .是 .是 Yes
Java Yes .是 .是 Yes
Node.js Yes .是 .是 Yes
Python Yes .是 .是 Yes
Yes .是 .是 Yes

下表指出支持數據表中所有用戶端類型和驗證方法的組合。 所有客戶端類型都可以使用任何驗證方法來使用 Service 連線 or 連線到 Azure 應用程式組態。

默認環境變數名稱或應用程式屬性和範例程序代碼

使用下列連線詳細數據,將計算服務連線到 Azure 應用程式組態 存放區。 如需命名慣例的詳細資訊,請參閱 Service 連線 or internals 一文。

系統指派的受控識別

默認環境變數名稱 描述 範例值
AZURE_APPCONFIGURATION_ENDPOINT 應用程式組態 端點 https://<App-Configuration-name>.azconfig.io

範例指令碼

請參閱下列步驟和程序代碼,以使用系統指派的受控識別連線到 Azure 應用程式組態。

  1. 安裝相依性。

    dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
    dotnet add package Azure.Identity
    
  2. 使用 Azure.Identity 進行驗證,並從 Service 連線 or 新增的環境變數取得 Azure 應用程式組態 端點。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。

    using Azure.Identity;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.AzureAppConfiguration;
    
    string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // system-assigned managed identity
    // var credential = new DefaultAzureCredential();
    
    // user-assigned managed identity
    // var credential = new DefaultAzureCredential(
    //     new DefaultAzureCredentialOptions
    //     {
    //         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTID");
    //     });
    
    // service principal 
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_TENANTID");
    // var clientId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTID");
    // var clientSecret = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTSECRET");
    // var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
    var client = new ConfigurationClient(new Uri(endpoint), credential);
    

使用者指派的受控識別

默認環境變數名稱 描述 範例值
AZURE_APPCONFIGURATION_ENDPOINT 應用程式組態 端點 https://App-Configuration-name>.azconfig.io
AZURE_APPCONFIGURATION_CLIENTID 您的用戶端識別碼 <client-ID>

範例指令碼

請參閱下列步驟和程序代碼,以使用使用者指派的受控識別連線到 Azure 應用程式組態。

  1. 安裝相依性。

    dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
    dotnet add package Azure.Identity
    
  2. 使用 Azure.Identity 進行驗證,並從 Service 連線 or 所新增的環境變數取得 Azure 應用程式組態 端點。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。

    using Azure.Identity;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.AzureAppConfiguration;
    
    string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // system-assigned managed identity
    // var credential = new DefaultAzureCredential();
    
    // user-assigned managed identity
    // var credential = new DefaultAzureCredential(
    //     new DefaultAzureCredentialOptions
    //     {
    //         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTID");
    //     });
    
    // service principal 
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_TENANTID");
    // var clientId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTID");
    // var clientSecret = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTSECRET");
    // var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
    var client = new ConfigurationClient(new Uri(endpoint), credential);
    

Connection string

默認環境變數名稱 描述 範例值
AZURE_APPCONFIGURATION_CONNECTIONSTRING 您的 應用程式組態 連線 字串 Endpoint=https://<App-Configuration-name>.azconfig.io;Id=<ID>;Secret=<secret>

範例程式碼

請參閱下面的步驟和程序代碼,以使用 連接字串 連線到 Azure 應用程式組態。

  1. 安裝相依性。

    dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
    
  2. 從 Service 連線 or 新增的環境變數取得 應用程式組態 連接字串。

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.AzureAppConfiguration;
    
    var connectionString = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CONNECTIONSTRING");
    var builder = new ConfigurationBuilder();
    builder.AddAzureAppConfiguration(connectionString);
    
    var config = builder.Build();
    

服務主體

默認環境變數名稱 描述 範例值
AZURE_APPCONFIGURATION_ENDPOINT 應用程式組態 端點 https://<AppConfigurationName>.azconfig.io
AZURE_APPCONFIGURATION_CLIENTID 您的用戶端識別碼 <client-ID>
AZURE_APPCONFIGURATION_CLIENTSECRET 您的客戶端密碼 <client-secret>
AZURE_APPCONFIGURATION_TENANTID 您的租用戶識別碼 <tenant-ID>

範例指令碼

請參閱下列步驟和程序代碼,以使用服務 principaL 連線到 Azure 應用程式組態。

  1. 安裝相依性。

    dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
    dotnet add package Azure.Identity
    
  2. 使用 Azure.Identity 進行驗證,並從 Service 連線 or 新增的環境變數取得 Azure 應用程式組態 端點。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。

    using Azure.Identity;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.AzureAppConfiguration;
    
    string endpoint = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_ENDPOINT");
    
    // Uncomment the following lines corresponding to the authentication type you want to use.
    // system-assigned managed identity
    // var credential = new DefaultAzureCredential();
    
    // user-assigned managed identity
    // var credential = new DefaultAzureCredential(
    //     new DefaultAzureCredentialOptions
    //     {
    //         ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTID");
    //     });
    
    // service principal 
    // var tenantId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_TENANTID");
    // var clientId = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTID");
    // var clientSecret = Environment.GetEnvironmentVariable("AZURE_APPCONFIGURATION_CLIENTSECRET");
    // var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    
    var client = new ConfigurationClient(new Uri(endpoint), credential);
    

下一步

請遵循下列教學課程,深入瞭解 Service 連線 or。