你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Application Insights 跟踪 Azure AD B2C 中的用户行为

重要

自 2025 年 5 月 1 日起,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 中提供新日志之前,延迟通常少于 5 分钟。
  • Azure AD B2C 允许选择要记录的声明。 不要在声明中包含个人数据。
  • 若要记录用户会话,可以使用关联 ID 来统一事件。
  • 直接从用户旅程子旅程调用 Application Insights 技术配置文件。 请勿使用 Application Insights 技术配置文件作为 验证技术配置文件

先决条件

创建 Application Insights 资源

将 Application Insights 与 Azure AD B2C 配合使用时,只需创建资源并获取连接字符串。 有关信息,请参阅 创建 Application Insights 资源

  1. 登录到 Azure 门户
  2. 如果有权访问多个租户,请选择顶部菜单中的“设置”图标,切换到“目录 + 订阅”菜单中的 Microsoft Entra ID 租户
  3. 在 Azure 门户的左上角选择 “创建资源 ”,然后搜索并选择 Application Insights
  4. 选择“ 创建”。
  5. 对于 “名称”,请输入资源的名称。
  6. 对于 应用程序类型,请选择 ASP.NET Web 应用程序
  7. 对于 资源组,请选择现有组或输入新组的名称。
  8. 选择“ 创建”。
  9. 打开新的 Application Insights 资源,展开 Essentials 并复制连接字符串。

显示“Application Insights 概述”选项卡上的连接字符串的屏幕截图。

定义权利要求

声明可在 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>
    

添加新的技术配置文件

可以将技术配置文件视为自定义策略中的函数。 这些函数使用技术配置文件包含的方法,即一个技术配置文件可以包含另一个技术配置文件,并更改设置或添加新功能。 下表定义了用于打开会话和发布事件的技术配置文件。

技术简介 任务
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 connection string, which you use for logging the events -->
        <Item Key="ConnectionString">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. 确定包含用户旅程的策略文件,例如 SocialAndLocalAccounts/SignUpOrSignin.xml,然后打开它。

  2. 调用 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>
    
  3. SendClaims 业务流程步骤之前,添加调用 AppInsights-UserSignup 的新步骤。 当用户在注册或登录旅程中选择注册按钮时,会触发它。 可能需要更新业务流程步骤 Order="8",以确保不会跳过第一个到最后一个业务流程步骤之间的任何整数。

    <!-- Handles the user selecting the sign-up link in the local account sign-in page 
     The `SendClaims` orchestration step comes after this one,
     -->
    <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>
    
  4. SendClaims 业务流程步骤后,调用 AppInsights-SignInComplete。 此步骤显示了成功完成的旅程。 可能需要更新编排步骤,Order="10",以确保不会从第一个到最后一个编排步骤跳过任何整数。

    <!-- Track that we have successfully sent a token 
     The `SendClaims` orchestration step come before this one,
     -->
    <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 分钟”。 可能需要刷新窗口才能查看结果。

显示 Application Insights 事件统计信息的屏幕截图。

收集更多数据

若要满足业务需求,可能需要记录更多声明。 若要添加声明,请先 定义声明,然后将声明添加到输入声明集合。 你添加到 AppInsights-Common 技术配置文件中的声明会显示在所有事件中。 你添加到特定技术配置文件的声明则仅显示在该事件中。 输入声明元素包含以下属性:

  • ClaimTypeReferenceId 是对声明类型的引用。
  • PartnerClaimType 是 Azure Insights 中显示的属性的名称。 使用语法{property:NAME},其中NAME是添加到事件的属性。
  • DefaultValue 是要记录的预定义值,例如事件名称。 如果用户旅程中使用的声明为空,则使用默认值。 例如,identityProvider 声明是由联合技术配置文件设置的,如 Facebook。 如果声明为空,则表示用户使用本地帐户登录。 因此,默认值设置为 “本地”。 还可以使用上下文值(例如应用程序 ID 或用户 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 技术配置文件的新技术配置文件。 然后,将新的技术配置文件作为业务流程步骤添加到用户旅程中。 准备就绪后,使用 Precondition 元素触发事件。 例如,仅当用户通过多重身份验证运行时报告该事件。

<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元数据更改为true

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

后续步骤

了解如何 使用 Azure Application Insights 创建自定义 KPI 仪表板