【问题标题】:Azure B2C - Accept query params into OAuth2 JWTAzure B2C - 接受查询参数到 OAuth2 JWT
【发布时间】:2018-07-12 08:37:07
【问题描述】:

我很好奇在通过 Azure 请求 OAuth2 令牌时是否可以读取查询参数?

基本上,当使用我创建的策略进行测试调用时,我希望从调用中读取额外的查询参数,并且编排(用户旅程)步骤应读取这些值并将该值注入自定义声明(对于 JWT 或 ID 令牌)。

我从以下链接知道 Azure B2C 服务可能*?但我找不到任何好的具体例子。

Sign-up policy - Set user attributes through code

Add Custom Attribute Not Used in Sign-Up nor Edit Policy

How can I return the PolicyId Claim after executing my Custom SignUpSignIn policy?

How do i include email in the redirect to AZURE AD B2C

然后我继续尝试了一堆配置,但有很多选项可供选择,我不知道该选择哪个。此外,我还没有找到任何描述配置这些策略时使用的选项的 Azure 文档。无论如何,这就是我所拥有的。

我从here 下载了 TrustFrameworkBase.xml 和 TrustFrameworkExtensions.xml。我从Azure doc 获得了这个 Github 链接,我还按照设置策略密钥的步骤操作,并添加了一个具有委派权限的应用程序注册。对于我的依赖方配置,我只是通过 Azure B2C 门户制定了一个自定义策略并将其下载为起点,以探索其基本形式的外观。

这是我添加到 ClaimsSchema 标记内的基本策略的自定义声明。 extension_Test 是我想从查询参数中注入值的声明:

  <ClaimType Id="extension_Test">
    <DisplayName>Test value</DisplayName>
    <DataType>string</DataType>
    <DefaultPartnerClaimTypes>
      <Protocol Name="OAuth2" PartnerClaimType="extension_Test" />
      <Protocol Name="OpenIdConnect" PartnerClaimType="extension_Test" />
    </DefaultPartnerClaimTypes>
    <UserInputType>Readonly</UserInputType>
  </ClaimType>
</ClaimsSchema>

在相同的基本策略中,这是我为登录添加的 userjourney:

<UserJourney Id="SignIn">
        <OrchestrationSteps>
            <OrchestrationSteps>
            <!-- The following orchestration step is always executed. -->
            <OrchestrationStep Order="1" Type="ClaimsProviderSelection" ContentDefinitionReferenceId="api.idpselection.signupsignin">
                <ClaimsProviderSelections>
                    <ClaimsProviderSelection TargetClaimsExchangeId="LocalAccountRegistrationExchange" />
                </ClaimsProviderSelections>
            </OrchestrationStep>
            <OrchestrationStep Order="2" Type="ClaimsExchange">
              <ClaimsExchanges>
                <ClaimsExchange Id="LocalAccountRegistrationExchange" TechnicalProfileReferenceId="LocalAccount-Registration-VerifiedEmail" />
              </ClaimsExchanges>
            </OrchestrationStep>
            <OrchestrationStep Order="3" Type="SendClaims" CpimIssuerTechnicalProfileReferenceId="JwtIssuer" />
        </OrchestrationSteps>

    </UserJourney>

这是我的依赖配置 XML:

 <RelyingParty>
    <DefaultUserJourney ReferenceId="SignIn" />
    <TechnicalProfile Id="PolicyProfile">
      <DisplayName>PolicyProfile</DisplayName>
      <Protocol Name="OpenIdConnect" />
      <InputClaims>
        <InputClaim ClaimTypeReferenceId="extension_Test" />
      </InputClaims>
      <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="displayName" />
        <OutputClaim ClaimTypeReferenceId="givenName" />
        <OutputClaim ClaimTypeReferenceId="surname" />
        <OutputClaim ClaimTypeReferenceId="extension_Test" />
        <OutputClaim ClaimTypeReferenceId="objectId" PartnerClaimType="sub" />
      </OutputClaims>
      <SubjectNamingInfo ClaimType="sub" />
    </TechnicalProfile>
  </RelyingParty>

我认为按此顺序上传了基本、扩展和 RP 策略 XML 文件。我发送的 GET 请求如下所示(来自自定义策略的“立即运行”按钮):

https://login.microsoftonline.com/<TENANT>/oauth2/v2.0/authorize?p=B2C_1A_test&client_id=<TENANTID>&nonce=defaultNonce&redirect_uri=http%3A%2F%2Flocalhost%2Fredirect&scope=openid&response_type=id_token&prompt=login&extension_Test=aaa

任何帮助将不胜感激,谢谢!或解释这些配置文件中更多选项的 Azure 文档 - 如 CpimIssuerTechnicalProfileReferenceId="JwtIssuer" 是什么意思?或者 AzureFunction-WrapWebHook 是什么意思?

【问题讨论】:

    标签: azure oauth-2.0 azure-active-directory claims-based-identity azure-ad-b2c


    【解决方案1】:

    你很接近。

    可以在this "Implementing an invitation flow" document(我是其中的作者)。

    高级解决方案是:

    1) 在设计时,使用输入声明配置依赖方策略。

    <RelyingParty>
      <DefaultUserJourney ReferenceId="SignIn" />
      <TechnicalProfile Id="PolicyProfile">
        <DisplayName>PolicyProfile</DisplayName>
        <Protocol Name="OpenIdConnect" />
        <InputTokenFormat>JWT</InputTokenFormat>
        <CryptographicKeys>
          <Key Id="client_secret" StorageReferenceId="B2C_1A_MySharedSecret" />
        </CryptographicKeys>
        <InputClaims>
          <InputClaim ClaimTypeReferenceId="extension_Test" />
        </InputClaims>
        <OutputClaims>
          ...
          <OutputClaim ClaimTypeReferenceId="extension_Test" />
        </OutputClaims>
        <SubjectNamingInfo ClaimType="sub" />
      </TechnicalProfile>
    </RelyingParty>
    

    您必须创建一个策略密钥(在上面的示例中,这称为“MySharedSecret”,但它可以称为任何名称),其中包含调用此策略的应用程序已知的共享密钥(其中客户端密钥用于这个应用程序可以是这个共享密钥)。

    2) 在运行时,创建一个包含输入声明的自发布 JWT,使用共享密钥对该 JWT 进行签名,然后使用“client_assertion_type”和“client_assertion”参数将 JWT 添加到身份验证请求中。

    这方面的代码示例可以在the Wingtip sample中找到。

    认证请求的一个例子是:

    https://login.microsoftonline.com/b2ctechready.onmicrosoft.com/oauth2/v2.0/authorize?p=b2c_1a_invitation&amp;...&amp;client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer&amp;client_assertion=eyJhbGci...7m9s&amp;state=CfDJ8EPk...Et0w

    【讨论】:

    • 谢谢@Chris Padgett。快速提问,有没有办法使用内置策略来使用 Azure AD 中的相同签名密钥,而不是策略的 JWK 端点?
    • 很遗憾,不,实际上不同的密钥由不同的 STS 管理。
    • @ChrisPadgett,我已经实现了邀请流程并且工作正常。我查看了 B2c 开发人员说明,发现客户端断言将被弃用,我们应该使用 id_token_hint(请参阅docs.microsoft.com/en-us/azure/active-directory-b2c/…)我尝试使用 id_token_hint 而不是 client_assertion 传递令牌,但它不起作用。你能帮我解决这个问题吗?有什么指导吗?谢谢
    • @ChrisPadgett 我在这里投票赞成你所有的答案,因为你是最能帮助解决与 Azure B2C 相关的问题的人。感谢您的努力。你帮了很多忙。出于同样的目的,请您在这里阐明一下吗? stackoverflow.com/q/57062803/114029
    猜你喜欢
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2020-05-10
    • 2021-04-23
    • 2019-04-19
    • 1970-01-01
    相关资源
    最近更新 更多