分享方式:


撰寫您的第一個 Azure Active Directory B2C 自定義原則 - Hello World!

在您的應用程式中,您可以使用使用者流程,讓用戶能夠註冊、登入或管理其配置檔。 當使用者流程未涵蓋所有商務特定需求時,您可以使用 自定義原則

雖然您可以使用預先建立的自定義原則 入門套件 來撰寫自定義原則,但請務必瞭解自定義原則的建置方式。 在本文中,您將瞭解如何從頭開始建立您的第一個自定義原則。

必要條件

注意

本文是 Azure Active Directory B2C 操作指南系列中建立和執行您自己的自定義原則的一部分。 建議您從第一篇文章開始此系列。

步驟 1 - 設定簽署和加密金鑰

如果您尚未這麼做,請建立下列加密密鑰。 若要自動化下列逐步解說,請流覽 IEF 安裝程式應用程式 ,並遵循指示:

  1. 使用新增身分識別體驗架構應用程式的簽署和加密金鑰中的步驟來建立簽署密鑰。

  2. 使用新增身分識別體驗架構應用程式的簽署和加密金鑰中的步驟來建立加密密鑰。

步驟 2 - 建置自定義原則檔案

  1. 在 VS Code 中,建立並開啟 檔案 ContosoCustomPolicy.XML

  2. 在 檔案中 ContosoCustomPolicy.XML ,新增下列程序代碼:

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <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_ContosoCustomPolicy"
          PublicPolicyUri="http://yourtenant.onmicrosoft.com/B2C_1A_ContosoCustomPolicy">
    
            <BuildingBlocks>
                <!-- Building Blocks Here-->
            </BuildingBlocks>
    
            <ClaimsProviders>
                <!-- Claims Providers Here-->
            </ClaimsProviders>
    
            <UserJourneys>
                <!-- User Journeys Here-->
            </UserJourneys>
    
            <RelyingParty>
                <!-- 
                    Relying Party Here that's your policy’s entry point
                    Specify the User Journey to execute 
                    Specify the claims to include in the token that is returned when the policy runs
                -->
            </RelyingParty>
        </TrustFrameworkPolicy>
    
    

    以租使用者名稱的子域部分取代 yourtenant ,例如 contoso。 瞭解如何 取得您的租用戶名稱

    XML 元素會使用原則標識碼和租用戶名稱來定義原則檔案的最上層 TrustFrameworkPolicy 專案。 TrustFrameworkPolicy 元素包含您將在此系列中使用的其他 XML 元素。

  3. 若要宣告宣告,請在 BuildingBlocks 檔案的 ContosoCustomPolicy.XML 區段中新增下列程序代碼:

      <ClaimsSchema>
        <ClaimType Id="objectId">
            <DisplayName>unique object Id for subject of the claims being returned</DisplayName>
            <DataType>string</DataType>
        </ClaimType>        
        <ClaimType Id="message">
            <DisplayName>Will hold Hello World message</DisplayName>
            <DataType>string</DataType>
        </ClaimType>
      </ClaimsSchema>
    

    宣告就像變數。 宣告的宣告也會顯示宣告的 數據類型

  4. 在 檔案的 ContosoCustomPolicy.XMLClaimsProviders段中,新增下列程序代碼:

        <ClaimsProvider>
          <DisplayName>Token Issuer</DisplayName>
          <TechnicalProfiles>
            <TechnicalProfile Id="JwtIssuer">
              <DisplayName>JWT Issuer</DisplayName>
              <Protocol Name="None" />
              <OutputTokenFormat>JWT</OutputTokenFormat>
              <Metadata>
                <Item Key="client_id">{service:te}</Item>
                <Item Key="issuer_refresh_token_user_identity_claim_type">objectId</Item>
                <Item Key="SendTokenResponseBodyWithJsonNumbers">true</Item>
              </Metadata>
              <CryptographicKeys>
                <Key Id="issuer_secret" StorageReferenceId="B2C_1A_TokenSigningKeyContainer" />
                <Key Id="issuer_refresh_token_key" StorageReferenceId="B2C_1A_TokenEncryptionKeyContainer" />
              </CryptographicKeys>
            </TechnicalProfile>
          </TechnicalProfiles>
        </ClaimsProvider>
    
        <ClaimsProvider>
          <!-- The technical profile(s) defined in this section is required by the framework to be included in all custom policies. -->
          <DisplayName>Trustframework Policy Engine TechnicalProfiles</DisplayName>
          <TechnicalProfiles>
            <TechnicalProfile Id="TpEngine_c3bd4fe2-1775-4013-b91d-35f16d377d13">
              <DisplayName>Trustframework Policy Engine Default Technical Profile</DisplayName>
              <Protocol Name="None" />
              <Metadata>
                <Item Key="url">{service:te}</Item>
              </Metadata>
            </TechnicalProfile>
          </TechnicalProfiles>
        </ClaimsProvider>
    

    我們已宣告 JWT 令牌簽發者。 在區CryptographicKeys段中,如果您使用不同的名稱來設定步驟 1 中的簽署和加密密鑰,請務必使用 正確的值。StorageReferenceId

  5. 在 檔案的 ContosoCustomPolicy.XMLUserJourneys段中,新增下列程序代碼:

      <UserJourney Id="HelloWorldJourney">
        <OrchestrationSteps>
          <OrchestrationStep Order="1" Type="SendClaims" CpimIssuerTechnicalProfileReferenceId="JwtIssuer" />
        </OrchestrationSteps>
      </UserJourney>
    

    我們已新增 UserJourney。 使用者旅程圖會指定終端使用者在 Azure AD B2C 處理要求時所經歷的商業規則。 此使用者旅程圖只有一個步驟會發出 JTW 令牌,其中包含您將在下一個步驟中定義的宣告。

  6. 在 檔案的 ContosoCustomPolicy.XMLRelyingParty段中,新增下列程序代碼:

      <DefaultUserJourney ReferenceId="HelloWorldJourney"/>
      <TechnicalProfile Id="HelloWorldPolicyProfile">
        <DisplayName>Hello World Policy Profile</DisplayName>
        <Protocol Name="OpenIdConnect" />
        <OutputClaims>
          <OutputClaim ClaimTypeReferenceId="objectId" PartnerClaimType="sub" DefaultValue="abcd-1234-efgh-5678-ijkl-etc."/>
          <OutputClaim ClaimTypeReferenceId="message" DefaultValue="Hello World!"/>
        </OutputClaims>
        <SubjectNamingInfo ClaimType="sub" />
      </TechnicalProfile>
    

    [ RelyingParty ] 區段是原則的進入點。 它會指定要 執行的 UserJourney ,以及要包含在原則執行時傳回之令牌中的宣告。

完成 步驟 2 之後, ContosoCustomPolicy.XML 檔案看起來應該類似下列程式代碼:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <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="Contosob2c2233.onmicrosoft.com" PolicyId="B2C_1A_ContosoCustomPolicy" PublicPolicyUri="http://Contosob2c2233.onmicrosoft.com/B2C_1A_ContosoCustomPolicy">
        <BuildingBlocks>
            <ClaimsSchema>
            <ClaimType Id="objectId">
                <DisplayName>unique object Id for subject of the claims being returned</DisplayName>
                <DataType>string</DataType>
            </ClaimType>        
            <ClaimType Id="message">
                <DisplayName>Will hold Hello World message</DisplayName>
                <DataType>string</DataType>
            </ClaimType>
            </ClaimsSchema>
        </BuildingBlocks>
        <ClaimsProviders><!--Claims Providers Here-->
            <ClaimsProvider>
                <DisplayName>Token Issuer</DisplayName>
                <TechnicalProfiles>
                    <TechnicalProfile Id="JwtIssuer">
                        <DisplayName>JWT Issuer</DisplayName>
                        <Protocol Name="None"/>
                        <OutputTokenFormat>JWT</OutputTokenFormat>
                        <Metadata>
                            <Item Key="client_id">{service:te}</Item>
                            <Item Key="issuer_refresh_token_user_identity_claim_type">objectId</Item>
                            <Item Key="SendTokenResponseBodyWithJsonNumbers">true</Item>
                        </Metadata>
                        <CryptographicKeys>
                            <Key Id="issuer_secret" StorageReferenceId="B2C_1A_TokenSigningKeyContainer"/>
                            <Key Id="issuer_refresh_token_key" StorageReferenceId="B2C_1A_TokenEncryptionKeyContainer"/>
                        </CryptographicKeys>
                    </TechnicalProfile>
                </TechnicalProfiles>
            </ClaimsProvider>
    
            <ClaimsProvider>
            <DisplayName>Trustframework Policy Engine TechnicalProfiles</DisplayName>
            <TechnicalProfiles>
                <TechnicalProfile Id="TpEngine_c3bd4fe2-1775-4013-b91d-35f16d377d13">
                <DisplayName>Trustframework Policy Engine Default Technical Profile</DisplayName>
                <Protocol Name="None" />
                <Metadata>
                    <Item Key="url">{service:te}</Item>
                </Metadata>
                </TechnicalProfile>
            </TechnicalProfiles>
            </ClaimsProvider>
        </ClaimsProviders>
      <UserJourneys>
        <UserJourney Id="HelloWorldJourney">
          <OrchestrationSteps>
            <OrchestrationStep Order="1" Type="SendClaims" CpimIssuerTechnicalProfileReferenceId="JwtIssuer" />
          </OrchestrationSteps>
        </UserJourney>
      </UserJourneys>
        <RelyingParty><!-- 
                Relying Party Here that's your policy’s entry point
                Specify the User Journey to execute 
                Specify the claims to include in the token that is returned when the policy runs
            -->
            <DefaultUserJourney ReferenceId="HelloWorldJourney"/>
            <TechnicalProfile Id="HelloWorldPolicyProfile">
                <DisplayName>Hello World Policy Profile</DisplayName>
                <Protocol Name="OpenIdConnect"/>
                <OutputClaims>
                    <OutputClaim ClaimTypeReferenceId="objectId" PartnerClaimType="sub" DefaultValue="abcd-1234-efgh-5678-ijkl-etc."/>
                    <OutputClaim ClaimTypeReferenceId="message" DefaultValue="Hello World!"/>
                </OutputClaims>
                <SubjectNamingInfo ClaimType="sub"/>
            </TechnicalProfile>
        </RelyingParty>
    </TrustFrameworkPolicy>

步驟 3 - 上傳自定義原則檔案

  1. 登入 Azure 入口網站
  2. 如果您有多個租使用者的存取權,請選取頂端功能表中的 設定 圖示,從 [目錄 + 訂用帳戶] 功能表切換至您的 Azure AD B2C 租使用者。
  3. 在 Azure 入口網站中,搜尋並選取 [Azure AD B2C]
  4. 在左側功能表中的 [原則] 底下,選取 [身分識別體驗架構]。
  5. 選取 [ 上傳自定義原則],瀏覽選取 ,然後上傳 ContosoCustomPolicy.XML 檔案。

上傳檔案之後,Azure AD B2C 會新增前置詞 B2C_1A_,因此名稱看起來類似 B2C_1A_CONTOSOCUSTOMPOLICY

步驟 4 - 測試自定義原則

  1. 在 [自定義原則] 底下,選取 [B2C_1A_CONTOSOCUSTOMPOLICY]。
  2. 針對在 自定義原則的概觀頁面上選取應用程式 ,請選取您先前註冊的 Web 應用程式,例如 webapp1 。 請確定 [選擇取回覆 URL ] 值已設定為https://jwt.ms
  3. 選取 [ 立即 執行] 按鈕。

原則完成執行之後,系統會將您重新導向至 https://jwt.ms,您會看到已譯碼的 JWT 令牌。 看起來類似下列 JWT 令牌代碼段:

    {
      "typ": "JWT",
      "alg": "RS256",
      "kid": "pxLOMWFg...."
    }.{
      ...
      "sub": "abcd-1234-efgh-5678-ijkl-etc.",
      ...
      "acr": "b2c_1a_contosocustompolicy",
      ...
      "message": "Hello World!"
    }.[Signature]

請注意 和 messagesub 宣告,我們在 區段中設定為輸出宣告RelyingParty

下一步

在本文中,您已瞭解和使用 Azure AD B2C 自定義原則中包含的四個區段。 這些區段會新增為根元素的 TrustFrameworkPolicy 子元素:

  • BuildingBlocks
  • ClaimsProviders
  • UserJourneys
  • RelyingParty

接下來,瞭解: