如何在 b2c 自定义策略中获取 OAuth2 持有者访问令牌以在登录注册自定义策略期间将声明发送到我的 REST API

how to get OAuth2 bearer access token in b2c custom policy to send claims to My REST API during signin sign up custom policy

我有 b2c 本地帐户登录注册流程使用自定义策略登录注册 我想添加一个对我的安全网站 API 的调用,以向此 API 发送用户声明。我已遵循此文档 Secure your API and access through custom policy 并使用 OAuth2 承载身份验证。为此,我已经按照上述文档注册了另一个应用程序,但我没有在不记名令牌声明(访问令牌)中获得任何不记名令牌。 当 运行 我的政策

时,我收到以下特定错误(在 jwt.ms 中)

错误:为不记名令牌指定的声明“bearerToken”不存在于可用声明中。

但是当我在用户旅程中添加技术配置文件“REST-AcquireAccessToken”时,我可以看到它正在返回 bearerToken 声明中的访问令牌。 但是当我在用户旅程中添加技术配置文件“REST-API-SignUp”时,出现以下错误:

我是否需要在用户旅程步骤中同时调用 REST 技术配置文件(REST-AcquireAccessToken、REST-API-SignUp)? 我尝试这样做,但未能上传政策。 任何帮助都感激不尽。 这些是我首先用于获取访问令牌的技术配置文件,第二个用于调用 API 发送用户声明

<TechnicalProfile Id="REST-AcquireAccessToken">
      <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/MyTenantName.onmicrosoft.com/oauth2/v2.0/token</Item>
        <Item Key="AuthenticationType">Basic</Item>
        <Item Key="SendClaimsIn">Form</Item>
      </Metadata>
     
        <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="https://MyTenantName.onmicrosoft.com/api/.default" />
</InputClaims>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="bearerToken" PartnerClaimType="access_token" />
      </OutputClaims>
      
      <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
    </TechnicalProfile>

   <!--talking To API-->
    <TechnicalProfile Id="REST-API-SignUp">
      <DisplayName>Send account details to API</DisplayName>
      <Protocol Name="Proprietary" Handler="Web.TPEngine.Providers.RestfulProvider, Web.TPEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <Metadata>
        <!-- Set the ServiceUrl with My REST API endpoint -->
        <Item Key="ServiceUrl">https://My API endPoint URL</Item>
        <Item Key="SendClaimsIn">Body</Item>
       
        <Item Key="AuthenticationType">Bearer</Item>
        <Item Key="UseClaimAsBearerToken">bearerToken</Item>
        <Item Key="AllowInsecureAuthInProduction">false</Item>
      </Metadata>
     
      <InputClaims>
        <!-- Claims Needs to sent to My REST API -->
        <InputClaim ClaimTypeReferenceId="bearerToken" />
        <InputClaim ClaimTypeReferenceId="email" />
        <InputClaim ClaimTypeReferenceId="givenName" />
        <InputClaim ClaimTypeReferenceId="surname" />
        <InputClaim ClaimTypeReferenceId="displayName" />
        </InputClaims>
       <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>


在用户旅程中,我在第 8 步中调用技术配置文件“REST-API-SignUp”,如下所示:

<OrchestrationStep Order="8" Type="ClaimsExchange">
              <ClaimsExchanges>
                <ClaimsExchange Id="RESTPostUserClaims" TechnicalProfileReferenceId="REST-API-SignUp" />
              </ClaimsExchanges>
            </OrchestrationStep> 

我是否还需要在用户日志步骤中调用 TechnicalProfile“REST-AcquireAccessToken”?如果是,那么什么时候?

感谢@Munazza Osama 的更新,在答案中发布解决方案。

解决方法: 错误:Claim "bearerToken" specified for the bearer token is not present in the available claims,已解决 调用“REST-AcquireAccessToken”技术配置文件获取访问令牌,并在用户旅程中调用“REST-API-SignUp”技术配置文件。

<OrchestrationStep Order="8" Type="ClaimsExchange">
         <ClaimsExchanges>
              <ClaimsExchange  Id="RESTGetAccessToken" TechnicalProfileReferenceId="REST-AcquireAccessToken " />
               <ClaimsExchange Id="RESTPostUserClaims" TechnicalProfileReferenceId="REST-API-SignUp" />
         </ClaimsExchanges>
            </OrchestrationStep>