Azure AD B2C 可以not当前接受用于填充用户配置文件属性的任何额外查询字符串参数。中有一个这样的请求Azure AD B2C UserVoice 论坛 https://feedback.azure.com/d365community/idea/9750399e-bf25-ec11-b6e6-000d3a4f0789.
但是,您可以通过使用图表在应用程序中自己实现它来获得相同的结果。
对于您的具体示例,您需要确保发送配置注册或注册/登录策略以发送 newUser 声明,然后在身份验证后使用该声明来调用图表并进行必要的更新。
以下是如何实现这一目标的示例,假设您按照以下方式使用 ASP.Net此登录示例 https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-web-dotnet or 此注册/登录示例 https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-web-dotnet-susi,通过在设置 OpenIdConnectAuthenticationOptions 时利用 SecurityTokenValidated 通知,如下所示:
new OpenIdConnectAuthenticationOptions
{
// Skipping for brevity
// (...)
Notifications = new OpenIdConnectAuthenticationNotifications
{
// (...)
SecurityTokenValidated = OnSecurityTokenValidated
},
// (...)
};
并使用 ClientCredentials 流程调用 Graph API 进行更新,如下所示:
private async Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
{
string userObjectId = notification.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
bool newUser = false;
bool.TryParse(notification.AuthenticationTicket.Identity.FindFirst("newUser")?.Value, out newUser);
if (!newUser) return;
ClientCredential credential = new ClientCredential(graphClientId, graphClientSecret);
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/sacacorpb2c.onmicrosoft.com");
AuthenticationResult result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", credential);
string body = "{ \"extension_e5bf5a2db0c9415cb62661a70d8f0a68_AccountId\" : \"Your_New_Value"\"}";
HttpClient http = new HttpClient();
string url = "https://graph.microsoft.com/beta/users/" + userObjectId + "/";
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), url)
{
Content = new StringContent(body, Encoding.UTF8, "application/json")
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await http.SendAsync(request);
return;
}
重要笔记:
- 如果要更新内置属性,可以使用 Azure AD Graph (https://graph.windows.net https://graph.windows.net),但是如果您想要更新自定义属性,则需要查询 Microsoft Graph 的 Beta 端点(https://graph.microsoft.com https://graph.microsoft.com)。如果您确实选择自定义属性,请注意它们具有更时髦的名称(以 Guid 开头),请使用图形浏览器 https://graphexplorer2.azurewebsites.net,查询 /beta/users 并查看完整的属性名称是什么。
- 您需要注册一个单独的应用程序(与您用于登录/注册的应用程序不同),并具有与图表对话的权限。看本文 https://learn.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet有关更多信息,尽管本文并未请求 Azure AD Graph 的权限,但您可能需要按照我之前的观点获取 Microsoft Graph 的权限。