我在 Azure AD B2C 上为基于 Angular 构建的 Web 应用程序配置了登录流程,该应用程序使用 msal 来设置配置和基于 ASP.NET Core API 构建的后端。 我通过以下步骤手动实施 Google 注册:
在后端生成授权URL并将其返回到前端以启动Google注册
{ "client_id", settings.Value.ClientId },
{ "redirect_uri", oauthRedirectUrl },
{ "response_type", "code" },
{ "scope", "openid email profile" },
{ "access_type", "offline" },
{ "state", state },
{ "prompt", "login" },
{ "nonce", "defaultNonce" },
{ "code_challenge_method", "S256" },
{ "code_challenge", codeChallenge }
选择帐户并重定向到重定向 uri 后,在后端验证状态并交换访问令牌的代码
从 Google /userinfo 端点获取用户个人资料信息。
在 Azure AD B2C 上创建用户帐户
public async Task<string> RegisterExternalUser(
string email,
CancellationToken cancellationToken
)
{
var userToCreate = new User
{
DisplayName = email,
Identities =
[
new()
{
SignInType = "federated",
Issuer = "google.com",
IssuerAssignedId = email
}
],
AccountEnabled = true,
};
try
{
var user = await graphServiceClient.Users.PostAsync(userToCreate, null, cancellationToken)
?? throw new Exception("User not found after registration");
return user.Id!;
}
catch (ODataError exception) when (exception.ResponseStatusCode == (int)HttpStatusCode.BadRequest)
{
throw new UserAlreadyRegisteredException(email);
}
}
问题如下。我已经在 Azure 上有一个通过电子邮件地址 SignInType 注册的注册用户。当此用户尝试使用同一电子邮件使用 Google 帐户注册时,会出现另一个 Azure 用户记录和另一个 IdpObjectId,这不是我想要的。为什么我有两条记录而不是一条? 我的猜测是,这与在步骤 3 中尝试通过 /userinfo 端点获取用户时 EmailVerified 属性为 false 有关,但不确定。
也许我做错了什么或者遗漏了一些部分,所以欢迎任何建议或评论!
这是因为 B2C 对待社交账户与本地账户的方式不同。
对于社交帐户,它会创建一个“影子”帐户。
如果查看自定义策略,本地帐户是通过“objectId”访问的,而社交帐户是通过“alternativeSecurityId”访问的。
如果您查看“身份”列下的“用户”选项卡,您会看到登录类型为“联合”,而对于本地,则为“电子邮件地址”或“用户名”。
如果您希望在同一用户帐户中使用两个身份,则必须链接它们。