首页 > 解决方案 > 如何获取自定义 B2C 策略以将 oauth2 承载(令牌)发送到我的自定义注册/登录 API

问题描述

我正在尝试让我的自定义 B2C 策略与我的自定义 SignUp/SignIn API 通信并通过 oauth2 承载(不是静态承载)进行身份验证。我已按照此处的说明进行操作:1(信中)但无法使其正常工作。我无法获取自定义策略以将带有不记名令牌的授权标头发送到我的 API,不确定原因。

这是我的配置:

<ClaimsProvider>
      <DisplayName>REST APIs</DisplayName>
      <TechnicalProfiles>

        <!--OAUTH2.0 customization START-->
        <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/mytenant.onmicrosoft.com/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="https://graph.microsoft.com/.default" />

          </InputClaims>
          <OutputClaims>
            <OutputClaim ClaimTypeReferenceId="bearerToken" PartnerClaimType="access_token" />
          </OutputClaims>
          <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />
        </TechnicalProfile>
        <!--OAUTH2.0 customization END-->

   <TechnicalProfile Id="REST-GetProfile">
      <DisplayName>Get user extended profile Azure Function web hook</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 your own REST API endpoint -->
        <Item Key="ServiceUrl">https://url_to_API/api/SignIn?</Item>
        <Item Key="SendClaimsIn">Body</Item>
        <Item Key="AuthenticationType">Bearer</Item>
        <Item Key="UseClaimAsBearerToken">bearerToken</Item>
        <Item Key="AllowInsecureAuthInProduction">false</Item>
      </Metadata>
      <InputClaims>
        <!-- Claims sent to your REST API -->
        <InputClaim ClaimTypeReferenceId="bearerToken"/>
        <InputClaim ClaimTypeReferenceId="objectId" />
        <InputClaim ClaimTypeReferenceId="extension_MemberId" />
        <InputClaim ClaimTypeReferenceId="email" />
      </InputClaims>
      <OutputClaims>
        <!-- Claims parsed from your REST API -->
        <OutputClaim ClaimTypeReferenceId="extension_MemberId" PartnerClaimType="extension_MemberId"/>
        <OutputClaim ClaimTypeReferenceId="email" />            
      </OutputClaims>
      <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop" />

文档中未提及但在帖子中看到的内容是,您需要在注册/登录过程中添加一个编排步骤以调用“获取访问令牌” - 但不是 100% 确定它需要它。

  <OrchestrationStep Order="5" Type="ClaimsExchange">
      <ClaimsExchanges>
        <ClaimsExchange  Id="RESTGetAccessToken" TechnicalProfileReferenceId="REST-AcquireAccessToken" />
      </ClaimsExchanges>
    </OrchestrationStep>
    <OrchestrationStep Order="6" Type="ClaimsExchange">
      <ClaimsExchanges>
        <ClaimsExchange Id="RESTGetProfile" TechnicalProfileReferenceId="REST-GetProfile" />
      </ClaimsExchanges>
    </OrchestrationStep>

我在没有配置授权的情况下检查了我的 API,并且可以看到自定义 B2C 策略正在调用它,但是我根本看不到任何承载令牌的证据(查看了 c# 代码中的 Request.Headers)。另外,我不确定如何配置我的 API 授权方面,文档中也省略了(不幸的是)。

有了以上内容,我是否遗漏了什么?我应该看到带有不记名令牌的授权标头吗?

很感谢任何形式的帮助!

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

解决方案


推荐阅读