使用 Application Insights 收集 Azure Active Directory B2C 記錄

開始之前,請使用 [選擇原則類型 選取器] 來選擇您要設定的原則類型。 Azure Active Directory B2C 提供兩種方法來定義使用者如何與您的應用程式互動:透過預先 定義的使用者流程 ,或透過完全可設定 的自定義原則。 本文中每個方法所需的步驟都不同。

此功能僅適用於自定義原則。 針對安裝步驟,請在上述選取器中選取 [自定義原則 ]。

本文提供從 Active Directory B2C (Azure AD B2C) 收集記錄的步驟,以便診斷自定義原則的問題。 Application Insights 提供方法來診斷例外狀況和將應用程式效能問題視覺化。 Azure AD B2C 包含將資料傳送至 Application Insights 的功能。

此處所述的詳細活動記錄應該只在開發自定義原則期間啟用

警告

請勿在生產環境中會設定 DeploymentModeDevelopment 。 記錄會收集傳送至識別提供者和從識別提供者傳送的所有宣告。 身為開發人員負責在Application Insights記錄中收集的任何個人資料。 只有在原則置於 開發人員模式時,才會收集這些詳細記錄。

設定 Application Insights

如果您還沒有訂用帳戶,請在您的訂用帳戶中建立Application Insights的實例。

提示

Application Insights 的單一實例可用於多個 Azure AD B2C 租使用者。 然後在查詢中,您可以依租用戶或原則名稱進行篩選。 如需詳細資訊, 請參閱 Application Insights 範例中的記錄。

若要在訂用帳戶中使用Application Insights的現有實例,請遵循下列步驟:

  1. 登入 Azure 入口網站
  2. 請確定您使用的是具有 Azure 訂用帳戶的 Microsoft Entra 目錄,而不是您的 Azure AD B2C 目錄。 在入口網站工具列中選取 [ 目錄 + 訂用帳戶 ] 圖示。
  3. 在入口 網站設定 |[目錄 + 訂 用帳戶] 頁面,在 [ 目錄名稱 ] 列表中尋找您的 Microsoft Entra 目錄,然後選取 [ 切換]。
  4. 開啟您稍早建立的 Application Insights 資源。
  5. 在 [概 觀] 頁面上,記錄 檢測密鑰

若要在訂用帳戶中建立Application Insights的實例,請遵循下列步驟:

  1. 登入 Azure 入口網站
  2. 請確定您使用的是具有 Azure 訂用帳戶的 Microsoft Entra 目錄,而不是您的 Azure AD B2C 目錄。 在入口網站工具列中選取 [ 目錄 + 訂用帳戶 ] 圖示。
  3. 在入口 網站設定 |[目錄 + 訂 用帳戶] 頁面,在 [ 目錄名稱 ] 列表中尋找您的 Microsoft Entra 目錄,然後選取 [ 切換]。
  4. 選取 左側導覽功能表中的 [建立資源 ]。
  5. 搜尋並選取 Application Insights,然後選取 [ 建立]。
  6. 完成窗體,選取 [ 檢閱 + 建立],然後選取 [ 建立]。
  7. 部署完成後,請選取 [移至資源]。
  8. [Application Insights] 功能表中的 [設定 ] 下,選取 [ 屬性]。
  9. 記錄檢測金鑰以供後續步驟使用。

設定自定義原則

  1. 開啟信賴憑證者 (RP) 檔案,例如 SignUpOrSignin.xml

  2. 將下列屬性新增至 <TrustFrameworkPolicy> 專案:

    DeploymentMode="Development"
    UserJourneyRecorderEndpoint="urn:journeyrecorder:applicationinsights"
    
  3. 如果尚未存在,請將 <UserJourneyBehaviors> 子節點新增至 <RelyingParty> 節點。 它必須位於 之後 <DefaultUserJourney ReferenceId="UserJourney Id" from your extensions policy, or equivalent (for example:SignUpOrSigninWithAAD" />。 如需 RelyingParty 子元素的完整順序,請參閱 RelyingParty 架構參考

  4. 將下列節點新增為 專案的子 <UserJourneyBehaviors> 系。 請務必將 取代 {Your Application Insights Key} 為您稍早記錄的 Application Insights 檢測密鑰

    <JourneyInsights TelemetryEngine="ApplicationInsights" InstrumentationKey="{Your Application Insights Key}" DeveloperMode="true" ClientEnabled="false" ServerEnabled="true" TelemetryVersion="1.0.0" />
    
    • DeveloperMode="true" 告知 ApplicationInsights 透過處理管線加速遙測。 適合開發,但受限於大量。 在生產環境中,將設定 DeveloperModefalse
    • ClientEnabled="true" 會傳送 ApplicationInsights 用戶端腳本,以追蹤頁面檢視和客戶端錯誤。 您可以在 Application Insights 入口網站的 browserTimings 數據表中檢視此專案。 藉由設定 ClientEnabled= "true",您會將Application Insights新增至頁面腳本,並取得頁面載入的時間,以及AJAX呼叫、計數、瀏覽器例外狀況的詳細數據,以及使用者和會話計數。 這個欄位是 選擇性的,且預設會設定為 false
    • ServerEnabled="true" 將現有的 UserJourneyRecorder JSON 傳送為 Application Insights 的自定義事件。

    例如:

    <TrustFrameworkPolicy
      ...
      TenantId="fabrikamb2c.onmicrosoft.com"
      PolicyId="SignUpOrSignInWithAAD"
      DeploymentMode="Development"
      UserJourneyRecorderEndpoint="urn:journeyrecorder:applicationinsights"
    >
    ...
    <RelyingParty>
      <DefaultUserJourney ReferenceId="UserJourney ID from your extensions policy, or equivalent (for example: SignUpOrSigninWithAzureAD)" />
      <Endpoints>
         <!--points to refresh token journey when app makes refresh token request-->
         <Endpoint Id="Token" UserJourneyReferenceId="RedeemRefreshToken" />
      </Endpoints>
      <UserJourneyBehaviors>
        <JourneyInsights TelemetryEngine="ApplicationInsights" InstrumentationKey="{Your Application Insights Key}" DeveloperMode="true" ClientEnabled="false" ServerEnabled="true" TelemetryVersion="1.0.0" />
      </UserJourneyBehaviors>
      ...
    </TrustFrameworkPolicy>
    
  5. 上傳原則。

查看 Application Insights 中的記錄

在 Application Insights 中看到新的記錄之前,延遲通常少於 5 分鐘。

  1. 開啟您在 Azure 入口網站 中建立的 Application Insights 資源。
  2. 在 [概 觀] 頁面上,選取 [ 記錄]。
  3. 在 Application Insights 中開啟新的索引標籤。

以下是可用來檢視記錄的查詢清單:

查詢 描述
traces 取得 Azure AD B2C 所產生的所有記錄
traces | where timestamp > ago(1d) 取得 Azure AD B2C 在過去一天所產生的所有記錄。
traces | where message contains "exception" | where timestamp > ago(2h) 取得過去兩個小時發生錯誤的所有記錄。
traces | where customDimensions.Tenant == "contoso.onmicrosoft.com" and customDimensions.UserJourney == "b2c_1a_signinandup" 取得 Azure AD B2C contoso.onmicrosoft.com 租使用者所產生的所有記錄,且使用者旅程圖b2c_1a_signinandup
traces | where customDimensions.CorrelationId == "00000000-0000-0000-0000-000000000000" 取得 Azure AD B2C 針對相互關聯標識碼所產生的所有記錄。 將相互關聯標識碼取代為您的相互關聯標識碼。

專案可能很長。 匯出至 CSV 以進一步瞭解。

如需查詢的詳細資訊,請參閱 Azure 監視器中的記錄查詢概觀。

請參閱 VS Code 擴充功能中的記錄

建議您安裝適用於 VS CodeAzure AD B2C 擴充功能。 使用 Azure AD B2C 擴充功能時,記錄會依原則名稱、相互關聯標識元(application insights 呈現相互關聯標識符的第一個數位)和記錄時間戳來為您組織。 此功能可協助您根據本機時間戳尋找相關的記錄,並查看 Azure AD B2C 所執行的使用者旅程圖。

注意

社群已開發 VS Code 擴充功能,以協助實作和維護 Azure AD B2C 解決方案的人員。 Microsoft 不支援此延伸模組,而且會嚴格依目前提供。

設定 Application Insights API 存取

設定 Application Insights 並設定自定義原則之後,您需要取得 Application Insights API 識別符,並建立 API 金鑰。 Azure AD B2C 擴充功能會同時使用 API 識別碼和 API 金鑰來讀取 Application Insights 事件(遙測)。 您的 API 金鑰應該像密碼一樣管理。 保密。

注意

Azure AD B2C 會使用您稍早建立的 Application Insights 檢測密鑰,將遙測數據傳送至 Application Insights。 您只能在 Azure AD B2C 原則中使用檢測金鑰,而不是在 VS Code 擴充功能中使用。

若要取得 Application Insights 識別碼和密鑰:

  1. 在 Azure 入口網站 中,開啟應用程式的Application Insights 資源。

  2. 選取 [ 設定],然後選取 [ API 存取]。

  3. 複製應用程式識別碼

  4. 選擇 [建立 API 金鑰]

  5. 核取 [ 讀取遙測] 方塊

  6. 請先複製 [ 金鑰 ],再關閉 [建立 API 金鑰] 刀鋒視窗,並將它儲存在安全的地方。 如果您遺失金鑰,則需要建立另一個金鑰。

    Screenshot that demonstrates how to create API access key.

設定 Azure AD B2C VS Code 擴充功能

既然您已 Azure 應用程式 深入解析 API 識別符和金鑰,您可以設定 VS Code 擴充功能來讀取記錄。 Azure AD B2C VS Code 擴充功能提供兩個設定範圍:

  • 用戶全域 設定 - 設定 套用至您開啟的任何 VS Code 實例。
  • 工作區 設定 - 設定 儲存在工作區內,且僅適用於開啟工作區時(使用 VS Code 開啟資料夾)。
  1. 從 Azure AD B2C 追蹤總管中,按兩下 設定 圖示。

    Screenshot that demonstrates select the application insights settings.

  2. 提供 Azure 應用程式 Insights 識別碼金鑰

  3. 按一下 [儲存]

儲存設定之後,Application insights 記錄會出現在 Azure AD B2C 追蹤 (App Insights) 視窗中。

Screenshot of Azure AD B2C extension for vscode, presenting the Azure Application insights trace.

在生產環境中設定Application Insights

若要改善生產環境效能和更好的用戶體驗,請務必設定原則來忽略不重要的訊息。 在生產環境中使用下列設定,且不會將任何記錄傳送至您的應用程式深入解析。

  1. DeploymentMode將 TrustFrameworkPolicy屬性設定為 Production

    <TrustFrameworkPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/online/cpim/schemas/2013/06" PolicySchemaVersion="0.3.0.0"
    TenantId="yourtenant.onmicrosoft.com"
    PolicyId="B2C_1A_signup_signin"
    PublicPolicyUri="http://yourtenant.onmicrosoft.com/B2C_1A_signup_signin"
    DeploymentMode="Production"
    UserJourneyRecorderEndpoint="urn:journeyrecorder:applicationinsights">
    
  2. DeveloperMode將 JourneyInsights 設定false

    <UserJourneyBehaviors>
      <JourneyInsights TelemetryEngine="ApplicationInsights" InstrumentationKey="{Your Application Insights Key}" DeveloperMode="false" ClientEnabled="false" ServerEnabled="true" TelemetryVersion="1.0.0" />
    </UserJourneyBehaviors>
    
  3. 上傳並測試您的原則。

下一步