Azure AD B2C:未注册用户重置密码时禁用验证码邮件
解决Azure B2C重置密码流程向未注册邮箱发送验证码的问题
核心方案
在发送验证码前先校验输入邮箱是否已在B2C目录中注册,若未注册则直接终止流程并返回错误,避免向无效邮箱发送验证码。
具体修改步骤
1. 添加用户存在验证的技术配置文件
在自定义策略的<ClaimsProviders>节点下,新增调用B2C账户存储的查询配置,用于验证邮箱对应的用户是否存在:
<ClaimsProvider> <DisplayName>Azure Active Directory</DisplayName> <TechnicalProfiles> <TechnicalProfile Id="AAD-UserReadUsingEmailAddress-Exists"> <Metadata> <Item Key="Operation">Read</Item> <!-- 开启用户不存在时的错误抛出 --> <Item Key="RaiseErrorIfClaimsPrincipalDoesNotExist">true</Item> </Metadata> <InputClaims> <InputClaim ClaimTypeReferenceId="email" PartnerClaimType="signInNames.emailAddress" Required="true" /> </InputClaims> <OutputClaims> <OutputClaim ClaimTypeReferenceId="objectId" /> </OutputClaims> <IncludeTechnicalProfile ReferenceId="AAD-Common" /> </TechnicalProfile> </TechnicalProfiles> </ClaimsProvider>
2. 修改重置密码用户旅程,插入校验步骤
找到Id为PasswordReset的<UserJourney>节点,在发送验证码的步骤之前插入用户存在校验的步骤:
<UserJourney Id="PasswordReset"> <OrchestrationSteps> <!-- 步骤1:收集用户输入的邮箱 --> <OrchestrationStep Order="1" Type="ClaimsExchange"> <ClaimsExchanges> <ClaimsExchange Id="PasswordResetUsingEmailAddressExchange" TechnicalProfileReferenceId="LocalAccountDiscoveryUsingEmailAddress" /> </ClaimsExchanges> </OrchestrationStep> <!-- 新增步骤2:校验用户是否存在 --> <OrchestrationStep Order="2" Type="ClaimsExchange"> <ClaimsExchanges> <ClaimsExchange Id="AAD-UserReadExistsExchange" TechnicalProfileReferenceId="AAD-UserReadUsingEmailAddress-Exists" /> </ClaimsExchanges> </OrchestrationStep> <!-- 原发送验证码步骤顺延为步骤3 --> <OrchestrationStep Order="3" Type="ClaimsExchange"> <ClaimsExchanges> <ClaimsExchange Id="PasswordResetVerifyCodeExchange" TechnicalProfileReferenceId="LocalAccountDiscoveryUsingEmailAddressExchange" /> </ClaimsExchanges> </OrchestrationStep> <!-- 后续重置密码步骤保持不变 --> ... </OrchestrationSteps> </UserJourney>
3. 配置用户不存在的错误提示
在<Localization>节点下添加自定义错误文案,优化用户体验:
<Localization Enabled="true"> <SupportedLanguages DefaultLanguage="zh-CN" MergeBehavior="ReplaceAll"> <SupportedLanguage>zh-CN</SupportedLanguage> </SupportedLanguages> <LocalizedResources Id="api.localaccountpasswordreset.zh-CN"> <LocalizedStrings> <LocalizedString ElementType="ErrorMessage" StringId="UserDoesNotExist">该邮箱未注册,请检查后重试。</LocalizedString> </LocalizedStrings> </LocalizedResources> </Localization>
关键说明
RaiseErrorIfClaimsPrincipalDoesNotExist设为true是核心逻辑:当查询不到对应用户时,B2C会自动终止流程并返回错误,不会继续执行发送验证码的步骤。- 确保输入邮箱的ClaimType映射正确,避免查询逻辑失效。
内容的提问来源于stack exchange,提问作者AS90
相关产品推荐
相关产品推荐

