重要
自 2025 年 5 月 1 日起,Azure AD B2C 将不再可供新客户购买。 在我们的常见问题解答中了解详细信息。
在开始之前,请使用此页面顶部的 “选择策略类型 选择器”来选择要设置的策略类型。 Azure Active Directory B2C 提供了两种定义用户如何与应用程序交互的方法:通过预定义的用户流,或者通过可完全配置的自定义策略。 对于每种方法,本文中所需的步骤都不同。
备注
此功能目前为公共预览版。
重要
从 2021 年 5 月开始,GitHub 宣布了一项会影响您的 Azure AD B2C 自定义策略联合的更改。 由于更改,请将 <Item Key="BearerTokenTransmissionMethod">AuthorizationHeader</Item>
元数据添加到 GitHub 的技术配置文件中。 有关详细信息,请参阅 通过查询参数弃用 API 身份验证。
- 创建用户流,以便用户能够注册并登录应用程序。
- 注册 Web 应用程序。
- 完成 Active Directory B2C 中的自定义策略入门中的步骤。 本教程指导你如何更新自定义策略文件以使用 Azure AD B2C 租户配置。
- 注册 Web 应用程序。
若要在 Azure Active Directory B2C(Azure AD B2C)中启用 GitHub 帐户登录,需要在 GitHub 开发人员 门户中创建应用程序。 有关详细信息,请参阅 创建 OAuth 应用。 如果您还没有 GitHub 帐户,您可以在 https://www.github.com/ 注册。
- 使用您的 GitHub 凭据登录 GitHub 开发者平台。
- 选择 OAuth 应用 ,然后选择 “新建 OAuth 应用”。
- 输入应用程序名称和主页 URL。
- 在授权回调 URL中输入
https://your-tenant-name.b2clogin.cn/your-tenant-name.partner.onmschina.cn/oauth2/authresp
。 将your-tenant-name
替换为租户的名称。 即便租户名称在 Azure AD B2C 中是用大写字母定义的,也请在输入时使用小写字母。 - 单击“注册应用程序”。
- 复制 客户端 ID 和 客户端机密的值。 将标识提供者添加到租户时需要这两个值。
- 使用至少具有外部标识提供者管理员权限的帐户登录到 Azure 门户。
- 如果有权访问多个租户,请选择顶部菜单中的“设置”图标,从“目录 + 订阅”菜单切换到你的 Azure AD B2C 租户。
- 选择 Azure 门户左上角的“所有服务” ,搜索并选择 Azure AD B2C。
- 选择“标识提供者”,然后选择“GitHub”(预览版)。
- 输入“名称”。 例如 ,GitHub。
- 对于 客户端 ID,请输入前面创建的 GitHub 应用程序的客户端 ID。
- 对于“客户端密码”,输入已记录的客户端密码。
- 选择“保存”。
此时,GitHub 标识提供者已设置,但尚未出现在任何登录页面上。 将 GitHub 标识提供者添加到用户流:
- 在 Azure AD B2C 租户中,选择“用户流” 。
- 单击要将 GitHub 标识提供者添加到的用户流。
- 在 “社交标识提供者”下,选择 “GitHub”。
- 选择“保存”。
- 若要测试策略,请选择“运行用户流”。
- 对于“应用程序”,请选择前面已注册的名为“testapp1”的 Web 应用程序。 “回复 URL”应显示为 。
- 选择“运行用户流”按钮。
- 在注册或登录页中,选择 GitHub 以使用 GitHub 帐户登录。
如果登录过程成功,浏览器将重定向到 https://jwt.ms
,后者显示 Azure AD B2C 返回的令牌内容。
需要存储以前在 Azure AD B2C 租户中记录的客户端密码。
- 登录到 Azure 门户。
- 如果有权访问多个租户,请选择顶部菜单中的“设置”图标,从“目录 + 订阅”菜单切换到你的 Azure AD B2C 租户。
- 选择 Azure 门户左上角的“所有服务”,然后搜索并选择“Azure AD B2C” 。
- 在“概述”页上选择“标识体验框架”。
- 选择“策略密钥”,然后选择“添加”。
- 对于“选项”,选择 。
Manual
- 输入策略密钥的名称。 例如,
GitHubSecret
。 前缀B2C_1A_
会自动添加到密钥名称。 - 在 “机密”中,输入之前记录的客户端密码。
- 在“密钥用法”处选择 。
- 单击 “创建” 。
若要允许用户使用 GitHub 帐户登录,需要将该帐户定义为可通过终结点与 Azure AD B2C 进行通信的声明提供者。 该终结点将提供一组声明,Azure AD B2C 使用这些声明来验证特定的用户是否已完成身份验证。
可以将 GitHub 帐户定义为声明提供程序,方法是将其添加到策略扩展文件中的 ClaimsProviders 元素。
打开 TrustFrameworkExtensions.xml。
找到 ClaimsProviders 元素。 如果该元素不存在,请在根元素下添加它。
添加一个新的 ClaimsProvider,如下所示:
<ClaimsProvider> <Domain>github.com</Domain> <DisplayName>GitHub</DisplayName> <TechnicalProfiles> <TechnicalProfile Id="GitHub-OAuth2"> <DisplayName>GitHub</DisplayName> <Protocol Name="OAuth2" /> <Metadata> <Item Key="ProviderName">github.com</Item> <Item Key="authorization_endpoint">https://github.com/login/oauth/authorize</Item> <Item Key="AccessTokenEndpoint">https://github.com/login/oauth/access_token</Item> <Item Key="ClaimsEndpoint">https://api.github.com/user</Item> <Item Key="HttpBinding">GET</Item> <Item Key="scope">read:user user:email</Item> <Item Key="UsePolicyInRedirectUri">0</Item> <Item Key="BearerTokenTransmissionMethod">AuthorizationHeader</Item> <Item Key="UserAgentForClaimsExchange">CPIM-Basic/{tenant}/{policy}</Item> <!-- Update the Client ID below to the Application ID --> <Item Key="client_id">Your GitHub application ID</Item> </Metadata> <CryptographicKeys> <Key Id="client_secret" StorageReferenceId="B2C_1A_GitHubSecret"/> </CryptographicKeys> <OutputClaims> <OutputClaim ClaimTypeReferenceId="displayName" PartnerClaimType="name" /> <OutputClaim ClaimTypeReferenceId="email" PartnerClaimType="email" /> <OutputClaim ClaimTypeReferenceId="numericUserId" PartnerClaimType="id" /> <OutputClaim ClaimTypeReferenceId="issuerUserId" /> <OutputClaim ClaimTypeReferenceId="identityProvider" DefaultValue="github.com" AlwaysUseDefaultValue="true" /> <OutputClaim ClaimTypeReferenceId="authenticationSource" DefaultValue="socialIdpAuthentication" AlwaysUseDefaultValue="true" /> </OutputClaims> <OutputClaimsTransformations> <OutputClaimsTransformation ReferenceId="CreateIssuerUserId" /> <OutputClaimsTransformation ReferenceId="CreateRandomUPNUserName"/> <OutputClaimsTransformation ReferenceId="CreateUserPrincipalName"/> <OutputClaimsTransformation ReferenceId="CreateAlternativeSecurityId"/> <OutputClaimsTransformation ReferenceId="CreateSubjectClaimFromAlternativeSecurityId"/> </OutputClaimsTransformations> <UseTechnicalProfileForSessionManagement ReferenceId="SM-SocialLogin" /> </TechnicalProfile> </TechnicalProfiles> </ClaimsProvider>
将client_id设置为应用程序注册中的应用程序 ID。
保存文件。
GitHub 技术配置文件要求 将 CreateIssuerUserId 声明转换添加到 ClaimsTransformations 列表。 如果没有在文件中定义的 ClaimsTransformations 元素,请添加父 XML 元素,如下所示。 声明转换还需要定义名为 numericUserId 的新声明类型。
- 搜索 BuildingBlocks 元素。 如果该元素不存在,请添加该元素。
- 找到 ClaimsSchema 元素。 如果该元素不存在,请添加该元素。
- 将 numericUserId 声明添加到 ClaimsSchema 元素。
- 找到 ClaimsTransformations 元素。 如果该元素不存在,请添加该元素。
- 将 CreateIssuerUserId 声明转换添加到 ClaimsTransformations 元素中。
<BuildingBlocks>
<ClaimsSchema>
<ClaimType Id="numericUserId">
<DisplayName>Numeric user Identifier</DisplayName>
<DataType>long</DataType>
</ClaimType>
</ClaimsSchema>
<ClaimsTransformations>
<ClaimsTransformation Id="CreateIssuerUserId" TransformationMethod="ConvertNumberToStringClaim">
<InputClaims>
<InputClaim ClaimTypeReferenceId="numericUserId" TransformationClaimType="inputClaim" />
</InputClaims>
<OutputClaims>
<OutputClaim ClaimTypeReferenceId="issuerUserId" TransformationClaimType="outputClaim" />
</OutputClaims>
</ClaimsTransformation>
</ClaimsTransformations>
</BuildingBlocks>
此时,标识提供者已设置,但还不能在任何登录页中使用。 如果你没有自己的自定义用户旅程,请创建现有模板用户旅程的副本,否则,请继续执行下一步。
- 打开初学者包中的 TrustFrameworkBase.xml 文件。
- 找到并复制包含 的
Id="SignUpOrSignIn"
元素的完整内容。 - 打开 TrustFrameworkExtensions.xml 并找到 UserJourneys 元素。 如果该元素不存在,请添加一个。
- 将您复制的 UserJourney 元素的完整内容粘贴到 UserJourneys 元素下作为其子项。
- 对用户旅程的 ID 进行重命名。 例如,
Id="CustomSignUpSignIn"
。
现在你已拥有用户旅程,请将新的身份提供者添加到用户旅程中。 首先添加一个“登录”按钮,然后将该按钮链接到某个操作。 该操作是前面创建的技术配置文件。
在用户旅程中,查找包含
Type="CombinedSignInAndSignUp"
或Type="ClaimsProviderSelection"
的业务流程步骤元素。 通常这是第一个编排步骤。 ClaimsProviderSelections 元素包含用户可以用来登录的标识提供者列表。 元素的顺序将决定向用户显示的登录按钮的顺序。 添加ClaimsProviderSelection XML 元素。 将 TargetClaimsExchangeId 的值设置为易记名称。在下一个编排步骤中,添加 ClaimsExchange 元素。 将 ID 设为目标声明交换 ID 的值。将 TechnicalProfileReferenceId 的值更新为先前创建的技术配置文件的 ID。
下面的 XML 演示了使用标识提供者进行用户旅程的前两个业务流程步骤:
<OrchestrationStep Order="1" Type="CombinedSignInAndSignUp" ContentDefinitionReferenceId="api.signuporsignin">
<ClaimsProviderSelections>
...
<ClaimsProviderSelection TargetClaimsExchangeId="GitHubExchange" />
</ClaimsProviderSelections>
...
</OrchestrationStep>
<OrchestrationStep Order="2" Type="ClaimsExchange">
...
<ClaimsExchanges>
<ClaimsExchange Id="GitHubExchange" TechnicalProfileReferenceId="GitHub-OAuth2" />
</ClaimsExchanges>
</OrchestrationStep>
信赖方策略(例如 SignUpSignIn.xml)指定 Azure AD B2C 将执行的用户旅程。 在信赖方内查找 DefaultUserJourney 元素。 更新 ReferenceId,使其与已在其中添加标识提供者的用户旅程 ID 匹配。
在以下示例中,对于 CustomSignUpSignIn
用户旅程,将 ReferenceId 设置为 CustomSignUpSignIn
:
<RelyingParty>
<DefaultUserJourney ReferenceId="CustomSignUpSignIn" />
...
</RelyingParty>
- 登录到 Azure 门户。
- 在门户工具栏中选择“目录 + 订阅”图标,然后选择包含 Azure AD B2C 租户的目录。
- 在 Azure 门户中,搜索并选择 Azure AD B2C。
- 在“策略”下,选择“Identity Experience Framework”。
- 选择“上传自定义策略”,然后上传已更改的两个策略文件,其顺序为:先上传扩展策略(例如 ),然后上传信赖方策略(例如
TrustFrameworkExtensions.xml
)。
- 选择依赖方策略,例如
B2C_1A_signup_signin
。 - 对于应用程序,请选择您前面注册的 Web 应用程序。 “回复 URL”应显示为 。
- 选择“立即运行”按钮。
- 在注册或登录页中,选择 GitHub 以使用 GitHub 帐户登录。
如果登录过程成功,浏览器将重定向到 https://jwt.ms
,后者显示 Azure AD B2C 返回的令牌内容。
- 了解如何将 GitHub 令牌传递给应用程序。
- 查看 GitHub 联合现场演示,以及如何传递 GitHub 访问令牌的现场演示