適用於 Application Insights 的 Microsoft Entra 驗證

Application Insights 現在支援 Microsoft Entra 驗證。 藉由使用 Microsoft Entra ID,您可以確保只有已驗證的遙測會內嵌在 Application Insights 資源中。

使用各種驗證系統可能會很麻煩且有風險,因為難以大規模管理認證。 您現在可以退出 宣告本機驗證 ,以確保只有使用 受控識別進行獨佔驗證的 遙測,而 Microsoft Entra ID 會內嵌在您的資源中。 這項功能是增強遙測的安全性和可靠性的步驟,可用來進行重大作業(警示自動調整)和商務決策。

注意

本文件涵蓋使用 Microsoft Entra ID 型驗證將數據擷取至 Application Insights。 如需在 Application Insights 內查詢數據的資訊,請參閱 使用 Microsoft Entra 驗證查詢 Application Insights。

必要條件

需要下列初步步驟,才能啟用 Microsoft Entra 驗證的擷取。 您需要:

不支援的情節

下列軟體開發工具組 (SDK) 和功能不受支援,無法與 Microsoft Entra 驗證的擷取搭配使用:

設定及啟用 Microsoft Entra 識別碼型驗證

  1. 如果您還沒有身分識別,請使用受控識別或服務主體來建立一個身分識別。

    • 我們建議使用受控識別:

      為您的 Azure 服務設定受控識別(虛擬機器 或 App Service)。

    • 不建議使用服務主體:

      如需如何建立可存取資源的 Microsoft Entra 應用程式和服務主體的詳細資訊,請參閱 建立服務主體

  2. 將角色指派給 Azure 服務。

    請遵循指派 Azure 角色中的步驟,將監視計量發行者角色從目標 Application Insights 資源新增至遙測傳送來源的 Azure 資源。

    注意

    雖然監視計量發行者角色會顯示「計量」,但它會將所有遙測發佈至 Application Insights 資源。

  3. 依照下列語言遵循設定指引。

Application Insights .NET SDK 支援 Azure 身分識別所提供的認證類別。

  • 建議您 DefaultAzureCredential 進行本機開發。
  • 我們建議 ManagedIdentityCredential 使用系統指派和使用者指派的受控識別。
    • 若為系統指派,請使用不含參數的預設建構函式。
    • 針對使用者指派,請將用戶端標識碼提供給建構函式。

下列範例示範如何使用 .NET 手動建立和設定 TelemetryConfiguration

TelemetryConfiguration.Active.ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/";
var credential = new DefaultAzureCredential();
TelemetryConfiguration.Active.SetAzureTokenCredential(credential);

下列範例示範如何使用 .NET Core 進行設定 TelemetryConfiguration

services.Configure<TelemetryConfiguration>(config =>
{
       var credential = new DefaultAzureCredential();
       config.SetAzureTokenCredential(credential);
});
services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
    ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/"
});

停用本機驗證

啟用 Microsoft Entra 驗證之後,您可以選擇停用本機驗證。 此設定可讓您擷取由 Microsoft Entra ID 獨佔驗證的遙測數據,並影響資料存取(例如,透過 API 金鑰)。

您可以使用 Azure 入口網站 或 Azure 原則 或以程式設計方式停用本機驗證。

Azure 入口網站

  1. 從 Application Insights 資源中,選取左側功能表中 [設定] 標題下的 [屬性]。 如果已啟用本機驗證,請選取 [已啟用] [按兩下以變更 ]。

    顯示 [設定] 區段下 [屬性] 和 [啟用] [選取變更] 本機驗證按鈕的螢幕快照。

  2. 選取 [已停用 ] 並套用變更。

    顯示本機驗證的螢幕快照,其中包含 [已啟用/停用] 按鈕。

  3. 停用資源上的本機驗證之後,您會在 [概觀] 窗格中看到對應的資訊。

    顯示 [概觀] 索引標籤的螢幕快照,其中包含 [已停用] [選取變更] 本機驗證按鈕。

Azure 原則

DisableLocalAuthAzure 原則 拒絕使用者建立新的 Application Insights 資源的能力,而不將此屬性設定為 true。 原則名稱為 Application Insights components should block non-AAD auth ingestion

若要將此原則定義套用至訂用帳戶,請建立新的原則指派並指派原則

下列範例顯示原則範本定義:

{
    "properties": {
        "displayName": "Application Insights components should block non-AAD auth ingestion",
        "policyType": "BuiltIn",
        "mode": "Indexed",
        "description": "Improve Application Insights security by disabling log ingestion that are not AAD-based.",
        "metadata": {
            "version": "1.0.0",
            "category": "Monitoring"
        },
        "parameters": {
            "effect": {
                "type": "String",
                "metadata": {
                    "displayName": "Effect",
                    "description": "The effect determines what happens when the policy rule is evaluated to match"
                },
                "allowedValues": [
                    "audit",
                    "deny",
                    "disabled"
                ],
                "defaultValue": "audit"
            }
        },
        "policyRule": {
            "if": {
                "allOf": [
                    {
                        "field": "type",
                        "equals": "Microsoft.Insights/components"
                    },
                    {
                        "field": "Microsoft.Insights/components/DisableLocalAuth",
                        "notEquals": "true"                        
                    }
                ]
            },
            "then": {
                "effect": "[parameters('effect')]"
            }
        }
    }
}

以程式設計方式啟用

屬性 DisableLocalAuth 可用來停用 Application Insights 資源上的任何本機驗證。 當此屬性設定為 true時,它會強制 Microsoft Entra 驗證必須用於所有存取。

下列範例顯示 Azure Resource Manager 範本,您可以用來建立已停用的工作區型 Application Insights 資源 LocalAuth

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "name": {
            "type": "string"
        },
        "type": {
            "type": "string"
        },
        "regionId": {
            "type": "string"
        },
        "tagsArray": {
            "type": "object"
        },
        "requestSource": {
            "type": "string"
        },
        "workspaceResourceId": {
            "type": "string"
        },
        "disableLocalAuth": {
            "type": "bool"
        }
     
    },
    "resources": [
        {
        "name": "[parameters('name')]",
        "type": "microsoft.insights/components",
        "location": "[parameters('regionId')]",
        "tags": "[parameters('tagsArray')]",
        "apiVersion": "2020-02-02-preview",
        "dependsOn": [],
        "properties": {
            "Application_Type": "[parameters('type')]",
            "Flow_Type": "Redfield",
            "Request_Source": "[parameters('requestSource')]",
            "WorkspaceResourceId": "[parameters('workspaceResourceId')]",
            "DisableLocalAuth": "[parameters('disableLocalAuth')]"
            }
    }
 ]
}

令牌物件

開發自定義用戶端以從 Microsoft Entra ID 取得存取令牌以將遙測提交至 Application Insights 時,請參閱下表,以判斷特定主機環境的適當物件字元串。

Azure 雲端版本 令牌物件值
Azure 公用雲端 https://monitor.azure.com
由 21Vianet 營運的 Microsoft Azure https://monitor.azure.cn
Azure 美國政府雲端 https://monitor.azure.us

如果您使用主權雲端,您也可以在 連接字串 中找到對象資訊。 連接字串 遵循下列結構:

InstrumentationKey={profile。InstrumentationKey};IngestionEndpoint={ingestionEndpoint};LiveEndpoint={liveDiagnosticsEndpoint};AADAudience={aadAudience}

audience 參數 AADAudience 可能會根據您的特定環境而有所不同。

疑難排解

本節提供不同的疑難解答案例和步驟,可讓您在提出支援票證之前解決問題。

擷取 HTTP 錯誤

不論 SDK 語言為何,擷取服務都會傳回特定錯誤。 您可以使用 Fiddler 之類的工具來收集網路流量。 您應該篩選 連接字串 中設定之擷取端點的流量。

不支援 HTTP/1.1 400 驗證

此錯誤顯示資源是針對僅限 Microsoft Entra 設定的。 您必須正確設定 SDK,因為它傳送到錯誤的 API。

注意

“v2/track” 不支援 Microsoft Entra ID。 正確設定 SDK 時,遙測會傳送至 「v2.1/track」。。

接下來,您應該檢閱 SDK 設定。

需要 HTTP/1.1 401 授權

此錯誤表示 SDK 已正確設定,但無法取得有效的令牌。 此錯誤可能表示 Microsoft Entra ID 有問題。

接下來,您應該識別來自 Azure 身分識別的 SDK 記錄或網路錯誤中的例外狀況。

HTTP/1.1 403 未經授權

此錯誤表示 SDK 在未經 Application Insights 資源或訂用帳戶許可權的情況下使用認證。

首先,檢查 Application Insights 資源的訪問控制。 您必須使用具有監視計量發行者角色的認證來設定 SDK。

語言特定疑難解答

事件來源

Application Insights .NET SDK 會使用事件來源發出錯誤記錄檔。 若要深入瞭解收集事件來源記錄,請參閱 針對沒有數據進行疑難解答 - 使用 PerfView 收集記錄。

如果 SDK 無法取得權杖,則會將例外狀況訊息記錄為 Failed to get AAD Token. Error message:

下一步