使用自定义策略获取受 B2C 保护的 api 的访问令牌

时间:2021-01-05 11:46:29

标签: azure-ad-b2c azure-ad-b2c-custom-policy

我有一个使用 ADB2C 身份验证保护的 API。我需要通过自定义策略调用这个 api。我遵循文档 enter link description here 并添加了两个技术配置文件作为自我声明配置文件的验证技术配置文件。

我收到了以下技术配置文件返回的访问令牌:

<TechnicalProfile Id="SecureREST-AccessToken">
      <DisplayName></DisplayName>
      <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <Metadata>
        <Item Key="ServiceUrl">https://login.microsoftonline.com/{tenant id here}/oauth2/v2.0/token</Item>
        <Item Key="AuthenticationType">Basic</Item>
        <Item Key="SendClaimsIn">Form</Item>
      </Metadata>
      <CryptographicKeys>
        <Key Id="BasicAuthenticationUsername" StorageReferenceId="B2C_1A_SecureRESTClientId" />
        <Key Id="BasicAuthenticationPassword" StorageReferenceId="B2C_1A_SecureRESTClientSecret" />
      </CryptographicKeys>
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="grant_type" DefaultValue="client_credentials" />
        <InputClaim ClaimTypeReferenceId="scope" DefaultValue="{app id uri for protected resource}/.default" />
      </InputClaims>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="bearerToken" PartnerClaimType="access_token" />
      </OutputClaims>
      <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
    </TechnicalProfile>

然后使用以下配置文件进行其余的 api 调用:

<TechnicalProfile Id="UserMigrationViaLegacyIdp">
                <DisplayName>REST API call to communicate with Legacy IdP</DisplayName>
                <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
                <Metadata>
                    <Item Key="ServiceUrl">
         
          https://99a0a14a6402.ngrok.io/api/Identity/SignUpAsync
        </Item>
                    <Item Key="AuthenticationType">Bearer</Item>
                    <Item Key="SendClaimsIn">Header</Item>
                    <Item Key="AllowInsecureAuthInProduction">false</Item>
        <Item Key="UseClaimAsBearerToken">bearerToken</Item>


      </Metadata>
                <InputClaims>
                    <InputClaim ClaimTypeReferenceId="bearerToken"/>
      </InputClaims>
                <OutputClaims>
                    
                    <OutputClaim ClaimTypeReferenceId="phonePresent"/>

                </OutputClaims>
                <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
            </TechnicalProfile>

但是,返回的访问令牌中缺少范围,因此 api 上的令牌验证失败。

我获取访问令牌的调用是否遗漏了什么?

1 个答案:

答案 0 :(得分:0)

对于客户端凭据授予流程,必须将 API 权限创建为角色(请参阅How to: Add app roles to your application and receive them in the token),然后授予管理员同意(请参阅Admin consent button)。

因此,不记名令牌包含 roles 声明,而不是 scp 声明。

API 应用程序使用此 roles 声明检查访问权限(请参阅 Verify app roles in APIs called by daemon apps)。