使用 Application Insights 追蹤 Azure AD B2C 中的用戶行為

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

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

在 Azure Active Directory B2C(Azure AD B2C)中,您可以使用提供給 Azure AD B2C 的檢測密鑰,將事件數據直接傳送至 Application Insights 。 使用 Application Insights 技術設定檔,您可以取得使用者旅程圖的詳細和自定義事件記錄檔:

  • 深入瞭解用戶行為。
  • 針對開發或生產環境中您自己的原則進行疑難解答。
  • 測量效能。
  • 從 Application Insights 建立通知。

概觀

若要啟用自定義事件記錄,請新增Application Insights技術配置檔。 在技術配置檔中,您會定義Application Insights 檢測金鑰、事件名稱和要記錄的宣告。 若要張貼事件,請在使用者旅程圖中將技術配置檔新增為協調流程步驟。

當您使用 Application Insights 時,請考慮下列事項:

  • 在 Application Insights 中提供新的記錄之前,延遲通常少於五分鐘。
  • Azure AD B2C 可讓您選擇要記錄的宣告。 請勿包含個人資料的宣告。
  • 若要記錄使用者會話,您可以使用相互關聯標識符來統一事件。
  • 直接從使用者旅程圖或子旅程圖呼叫 Application Insights 技術配置檔。 請勿使用 Application Insights 技術配置檔作為 驗證技術配置檔

必要條件

建立 Application Insights 資源

當您搭配 Azure AD B2C 使用 Application Insights 時,您只需要建立資源並取得檢測密鑰。 如需詳細資訊,請參閱 建立 Application Insights 資源

  1. 登入 Azure 入口網站
  2. 如果您有多個租使用者的存取權,請選取頂端功能表中的 [設定] 圖示,以從 [目錄 + 訂用帳戶] 功能表切換至您的 Microsoft Entra ID 租使用者。
  3. 選擇 [Azure 入口網站 左上角的 [建立資源],然後搜尋並選取 [Application Insights]。
  4. 選取建立
  5. 針對 [ 名稱],輸入資源的名稱。
  6. 針對 [應用程式類型],選取 [ASP.NET Web 應用程式]。
  7. 針對 [ 資源群組],選取現有的群組或輸入新群組的名稱。
  8. 選取建立
  9. 開啟新的 Application Insights 資源,展開 [基本資訊],然後複製檢測密鑰。

Screenshot that shows the Instrumentation Key on the Application Insights Overview tab.

定義宣告

宣告會在 Azure AD B2C 原則執行期間提供數據的暫存記憶體。 您會在 ClaimsSchema 元素宣告宣告宣告。

  1. 開啟原則的延伸模組檔案。 檔案看起來可能類似 SocialAndLocalAccounts/TrustFrameworkExtensions.xml

  2. 搜尋 BuildingBlocks 元素。 如果您沒有看到元素,請加以新增。

  3. 尋找 ClaimsSchema 元素。 如果您沒有看到元素,請加以新增。

  4. 將下列宣告新增至 ClaimsSchema 元素:

    <ClaimType Id="EventType">
      <DisplayName>Event type</DisplayName>
      <DataType>string</DataType>
    </ClaimType>
    <ClaimType Id="EventTimestamp">
      <DisplayName>Event timestamp</DisplayName>
      <DataType>string</DataType>
    </ClaimType>
    <ClaimType Id="PolicyId">
      <DisplayName>Policy Id</DisplayName>
      <DataType>string</DataType>
    </ClaimType>
    <ClaimType Id="Culture">
      <DisplayName>Culture ID</DisplayName>
      <DataType>string</DataType>
    </ClaimType>
    <ClaimType Id="CorrelationId">
      <DisplayName>Correlation Id</DisplayName>
      <DataType>string</DataType>
    </ClaimType>
    <ClaimType Id="federatedUser">
      <DisplayName>Federated user</DisplayName>
      <DataType>boolean</DataType>
    </ClaimType>
    <ClaimType Id="parsedDomain">
      <DisplayName>Domain name</DisplayName>
      <DataType>string</DataType>
      <UserHelpText>The domain portion of the email address.</UserHelpText>
    </ClaimType>
    <ClaimType Id="userInLocalDirectory">
      <DisplayName>userInLocalDirectory</DisplayName>
      <DataType>boolean</DataType>
    </ClaimType>
    

新增技術配置檔

技術配置檔可視為自定義原則中的函式。 這些函式會 使用技術配置檔包含 方法,其中技術配置檔包含另一個技術配置檔和變更設定,或新增新功能。 下表定義用來開啟會話和張貼事件的技術配置檔。

技術配置檔 Task
AppInsights-Common 具有一般設定的常見技術配置檔。 其中包含 Application Insights 檢測金鑰、要記錄的宣告集合,以及開發人員模式。 其他技術配置檔包括常見的技術配置檔,並新增更多宣告,例如事件名稱。
AppInsights-SignInRequest 在收到登入要求時,記錄具有一組宣告的 SignInRequest 事件。
AppInsights-UserSignUp 當用戶在註冊或登入旅程圖中觸發註冊選項時,記錄 UserSignUp 事件。
AppInsights-SignInComplete 在成功驗證時記錄 SignInComplete 事件,當令牌已傳送至信賴憑證者應用程式時。

從入門套件開啟 TrustFrameworkExtensions.xml 檔案。 將技術設定檔新增至 ClaimsProvider 元素:

<ClaimsProvider>
  <DisplayName>Application Insights</DisplayName>
  <TechnicalProfiles>
    <TechnicalProfile Id="AppInsights-Common">
      <DisplayName>Application Insights</DisplayName>
      <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.Insights.AzureApplicationInsightsProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <Metadata>
        <!-- The ApplicationInsights instrumentation key, which you use for logging the events -->
        <Item Key="InstrumentationKey">xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</Item>
        <Item Key="DeveloperMode">false</Item>
        <Item Key="DisableTelemetry ">false</Item>
      </Metadata>
      <InputClaims>
        <!-- Properties of an event are added through the syntax {property:NAME}, where NAME is the property being added to the event. DefaultValue can be either a static value or a value that's resolved by one of the supported DefaultClaimResolvers. -->
        <InputClaim ClaimTypeReferenceId="EventTimestamp" PartnerClaimType="{property:EventTimestamp}" DefaultValue="{Context:DateTimeInUtc}" />
        <InputClaim ClaimTypeReferenceId="tenantId" PartnerClaimType="{property:TenantId}" DefaultValue="{Policy:TrustFrameworkTenantId}" />
        <InputClaim ClaimTypeReferenceId="PolicyId" PartnerClaimType="{property:Policy}" DefaultValue="{Policy:PolicyId}" />
        <InputClaim ClaimTypeReferenceId="CorrelationId" PartnerClaimType="{property:CorrelationId}" DefaultValue="{Context:CorrelationId}" />
        <InputClaim ClaimTypeReferenceId="Culture" PartnerClaimType="{property:Culture}" DefaultValue="{Culture:RFC5646}" />
      </InputClaims>
    </TechnicalProfile>

    <TechnicalProfile Id="AppInsights-SignInRequest">
      <InputClaims>
        <!-- An input claim with a PartnerClaimType="eventName" is required. This is used by the AzureApplicationInsightsProvider to create an event with the specified value. -->
        <InputClaim ClaimTypeReferenceId="EventType" PartnerClaimType="eventName" DefaultValue="SignInRequest" />
      </InputClaims>
      <IncludeTechnicalProfile ReferenceId="AppInsights-Common" />
    </TechnicalProfile>

    <TechnicalProfile Id="AppInsights-UserSignUp">
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="EventType" PartnerClaimType="eventName" DefaultValue="UserSignUp" />
      </InputClaims>
      <IncludeTechnicalProfile ReferenceId="AppInsights-Common" />
    </TechnicalProfile>

    <TechnicalProfile Id="AppInsights-SignInComplete">
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="EventType" PartnerClaimType="eventName" DefaultValue="SignInComplete" />
        <InputClaim ClaimTypeReferenceId="federatedUser" PartnerClaimType="{property:FederatedUser}" DefaultValue="false" />
        <InputClaim ClaimTypeReferenceId="parsedDomain" PartnerClaimType="{property:FederationPartner}" DefaultValue="Not Applicable" />
        <InputClaim ClaimTypeReferenceId="identityProvider" PartnerClaimType="{property:IDP}" DefaultValue="Local" />
      </InputClaims>
      <IncludeTechnicalProfile ReferenceId="AppInsights-Common" />
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>

重要

將技術配置檔中的 AppInsights-Common 檢測金鑰變更為 Application Insights 資源提供的 GUID。

將技術配置檔新增為協調流程步驟

新增參考技術配置文件的協調流程步驟。

重要

新增協調流程步驟之後,請循序重新編號步驟,而不會略過從 1 到 N 的任何整數。

  1. 呼叫 AppInsights-SignInRequest 作為第二個協調流程步驟。 此步驟會追蹤已收到註冊或登入要求。

    <!-- Track that we have received a sign in request -->
    <OrchestrationStep Order="2" Type="ClaimsExchange">
      <ClaimsExchanges>
        <ClaimsExchange Id="TrackSignInRequest" TechnicalProfileReferenceId="AppInsights-SignInRequest" />
      </ClaimsExchanges>
    </OrchestrationStep>
    
  2. SendClaims 協調流程步驟之前,新增呼叫 AppInsights-UserSignup的新步驟。 當用戶在註冊或登入旅程圖中選取 [註冊] 按鈕時,就會觸發它。

    <!-- Handles the user selecting the sign-up link in the local account sign-in page -->
    <OrchestrationStep Order="8" Type="ClaimsExchange">
      <Preconditions>
        <Precondition Type="ClaimsExist" ExecuteActionsIf="false">
          <Value>newUser</Value>
          <Action>SkipThisOrchestrationStep</Action>
        </Precondition>
        <Precondition Type="ClaimEquals" ExecuteActionsIf="true">
          <Value>newUser</Value>
          <Value>false</Value>
          <Action>SkipThisOrchestrationStep</Action>
        </Precondition>
      </Preconditions>
      <ClaimsExchanges>
        <ClaimsExchange Id="TrackUserSignUp" TechnicalProfileReferenceId="AppInsights-UserSignup" />
      </ClaimsExchanges>
    </OrchestrationStep>
    
  3. SendClaims 協調流程步驟之後,呼叫 AppInsights-SignInComplete。 此步驟會顯示成功完成的旅程。

    <!-- Track that we have successfully sent a token -->
    <OrchestrationStep Order="10" Type="ClaimsExchange">
      <ClaimsExchanges>
        <ClaimsExchange Id="TrackSignInComplete" TechnicalProfileReferenceId="AppInsights-SignInComplete" />
      </ClaimsExchanges>
    </OrchestrationStep>
    

上傳檔案、執行原則及檢視事件

儲存並上傳 TrustFrameworkExtensions.xml 檔案。 然後從您的應用程式呼叫信賴憑證者原則,或使用 Azure 入口網站 中的 [立即執行]。 等候 Application Insights 中提供您的事件。

  1. 在您的 Microsoft Entra 租用戶中開啟 Application Insights 資源。
  2. 選取 [ 使用量],然後選取 [ 事件]。
  3. 將 [期間] 設定為 [過去一小時] 和 [ 3 分鐘]。 您可能需要重新整理視窗才能看到結果。

Screenshot that shows Application Insights event statistics.

收集更多數據

若要符合您的業務需求,您可能想要記錄更多宣告。 若要新增宣告,請先 定義宣告,然後將宣告新增至輸入宣告集合。 您新增至 AppInsights-Common 技術配置檔的宣告會出現在所有事件中。 您新增至特定技術配置檔的宣告只會出現在該事件中。 輸入宣告專案包含下列屬性:

  • ClaimTypeReferenceId 是宣告類型的參考。
  • PartnerClaimType 是出現在 Azure Insights 中的屬性名稱。 使用的 {property:NAME}語法,其中 NAME 是要加入至 事件的屬性。
  • DefaultValue 是要記錄的預先定義值,例如事件名稱。 如果使用者旅程圖中使用的宣告是空的,則會使用預設值。 例如, identityProvider 宣告是由同盟技術配置檔所設定,例如 Facebook。 如果宣告是空的,表示使用者使用本機帳戶登入。 因此,預設值會設定為 Local。 您也可以使用內容值來記錄 宣告解析程式 ,例如應用程式識別碼或使用者IP位址。

操作宣告

您可以使用 輸入宣告轉換 來修改輸入宣告或產生新的宣告,再將它們傳送至 Application Insights。 在下列範例中,技術配置檔包含 CheckIsAdmin 輸入宣告轉換。

<TechnicalProfile Id="AppInsights-SignInComplete">
  <InputClaimsTransformations>  
    <InputClaimsTransformation ReferenceId="CheckIsAdmin" />
  </InputClaimsTransformations>
  <InputClaims>
    <InputClaim ClaimTypeReferenceId="isAdmin" PartnerClaimType="{property:IsAdmin}"  />
    ...
  </InputClaims>
  <IncludeTechnicalProfile ReferenceId="AppInsights-Common" />
</TechnicalProfile>

新增事件

若要新增事件,請建立包含 AppInsights-Common 技術配置文件的新技術配置檔。 然後將新的技術配置檔新增為使用者旅程圖協調流程步驟。 當您準備好時, 請使用前置條件 元素來觸發事件。 例如,只有在使用者透過多重要素驗證執行時,才報告事件。

<TechnicalProfile Id="AppInsights-MFA-Completed">
  <InputClaims>
     <InputClaim ClaimTypeReferenceId="EventType" PartnerClaimType="eventName" DefaultValue="MFA-Completed" />
  </InputClaims>
  <IncludeTechnicalProfile ReferenceId="AppInsights-Common" />
</TechnicalProfile>

重要

當您將事件新增至使用者旅程圖時,請記得循序重新編號協調流程步驟。

<OrchestrationStep Order="8" Type="ClaimsExchange">
  <Precondition Type="ClaimsExist" ExecuteActionsIf="true">
    <Value>isActiveMFASession</Value>
    <Action>SkipThisOrchestrationStep</Action>
    </Precondition>
  </Preconditions>
  <ClaimsExchanges>
    <ClaimsExchange Id="TrackUserMfaCompleted" TechnicalProfileReferenceId="AppInsights-MFA-Completed" />
  </ClaimsExchanges>
</OrchestrationStep>

啟用開發人員模式

當您使用 Application Insights 來定義事件時,可以指出是否啟用開發人員模式。 開發人員模式會控制事件的緩衝處理方式。 在最少事件量的開發環境中,啟用開發人員模式會導致立即將事件傳送至 Application Insights。 預設值是 false。 請勿在生產環境中啟用開發人員模式。

若要啟用開發人員模式,請將 DeveloperMode 中繼資料變更為 true 技術設定檔中的 AppInsights-Common

<TechnicalProfile Id="AppInsights-Common">
  <Metadata>
    ...
    <Item Key="DeveloperMode">true</Item>
  </Metadata>
</TechnicalProfile>

停用遙測

若要停用 Application Insights 記錄,請將 DisableTelemetry 技術設定檔中的中繼資料變更為 trueAppInsights-Common

<TechnicalProfile Id="AppInsights-Common">
  <Metadata>
    ...
    <Item Key="DisableTelemetry">true</Item>
  </Metadata>
</TechnicalProfile>

下一步

瞭解如何 使用 Azure 應用程式 Insights 建立自訂 KPI 儀表板。