첫 번째 Azure Active Directory B2C 사용자 지정 정책 작성 - Hello World!

애플리케이션에는 사용자가 자신의 프로필을 등록, 로그인 또는 관리할 수 있도록 하는 사용자 흐름을 사용할 수 있습니다. 사용자 흐름이 모든 비즈니스 관련 요구 사항을 충족하지 않는 경우 사용자 지정 정책을 사용할 수 있습니다.

미리 만들어진 사용자 지정 정책 스타터 팩을 사용하여 사용자 지정 정책을 작성할 수 있지만 사용자 지정 정책이 빌드되는 방식을 이해하는 것이 중요합니다. 이 문서에서는 첫 번째 사용자 지정 정책을 처음부터 만드는 방법을 알아봅니다.

필수 조건

참고 항목

이 문서는 Azure Active Directory B2C 방법 가이드 시리즈에서 사용자 고유의 사용자 지정 정책 만들기 및 실행의 일부입니다. 이 시리즈는 첫 번째 문서부터 시작하는 것이 좋습니다.

1단계 - 서명 및 암호화 키 구성

아직 수행하지 않은 경우 다음 암호화 키를 만듭니다. 아래 연습을 자동화하려면 IEF 설치 앱을 방문하여 지침을 따르세요.

  1. Identity Experience Framework 애플리케이션에 대한 서명 및 암호화 키 추가의 단계를 사용하여 서명 키를 만듭니다.

  2. Identity Experience Framework 애플리케이션에 대한 서명 및 암호화 키 추가의 단계를 사용하여 암호화 키를 만듭니다.

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 요소는 정책 ID 및 테넌트 이름을 사용하여 정책 파일의 최상위 TrustFrameworkPolicy 요소를 정의합니다. TrustFrameworkPolicy 요소에는 이 시리즈에서 사용할 다른 XML 요소가 포함되어 있습니다.

  3. 클레임을 선언하려면 ContosoCustomPolicy.XML 파일의 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>
    

    클레임은 변수와 같습니다. 클레임의 선언에는 클레임의 데이터 형식도 표시됩니다.

  4. ContosoCustomPolicy.XML 파일의 ClaimsProviders 섹션에서 다음 코드를 추가합니다.

        <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.XML 파일의 UserJourneys 섹션에서 다음 코드를 추가합니다.

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

    UserJourney를 추가했습니다. 사용자 경험은 Azure AD B2C가 요청을 처리할 때 최종 사용자가 거쳐야 하는 비즈니스 논리를 지정합니다. 이 사용자 경험에는 다음 단계에서 정의할 클레임으로 JTW 토큰을 발급하는 단계가 하나뿐입니다.

  6. ContosoCustomPolicy.XML 파일의 RelyingParty 섹션에서 다음 코드를 추가합니다.

      <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 Portal에 로그인합니다.
  2. 여러 테넌트에 액세스할 수 있는 경우 상단 메뉴의 설정 아이콘을 선택하여 디렉터리 + 구독 메뉴에서 Azure AD B2C 테넌트로 전환합니다.
  3. Azure Portal에서 Azure AD B2C를 검색하고 선택합니다.
  4. 왼쪽 메뉴의 정책 아래에서 Identity Experience Framework를 선택합니다.
  5. 사용자 지정 정책 업로드를 선택하고 찾아보기를 선택한 다음, ContosoCustomPolicy.XML 파일을 업로드합니다.

파일을 업로드한 후 Azure AD B2C는 접두사 B2C_1A_를 추가하므로 이름은 B2C_1A_CONTOSOCUSTOMPOLICY와 비슷합니다.

4단계 - 사용자 지정 정책 테스트

  1. 사용자 지정 정책에서 B2C_1A_CONTOSOCUSTOMPOLICY를 선택합니다.
  2. 사용자 지정 정책의 개요 페이지에 있는 애플리케이션 선택에서 이전에 등록한 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]

RelyingParty 섹션에서 출력 클레임으로 설정한 messagesub 클레임을 확인합니다.

다음 단계

이 문서에서는 Azure AD B2C 사용자 지정 정책에 포함된 네 가지 섹션을 학습하고 사용했습니다. 이러한 섹션은 TrustFrameworkPolicy 루트 요소에 자식 요소로 추가됩니다.

  • BuildingBlocks
  • ClaimsProviders
  • UserJourneys
  • RelyingParty

다음으로, 다음을 알아봅니다.