Azure AD B2C不會「自動填充」任何字段。
當您設置註冊策略或統一註冊/登錄策略時,您可以選擇註冊屬性。這些屬性顯示給用戶供他/她提供,然後存儲在Azure AD B2C中。
任何不提示用戶的東西都會留空或在一些選擇的情況下(如您所觀察到的名稱)設置爲「未知」。
Azure AD B2C無法對預填充給定屬性做出假設。雖然您可能會認爲使用電子郵件作爲名稱的默認值是可以接受的,但其他人可能不會。另一個例子,對於某些人來說,顯示名稱可以預先填入「{Given name} {姓氏}」,但對其他人來說,這是另一種方式「{姓氏,姓名}」。
您實際上要求的是一種簡單的方法來配置某些屬性的默認值,這些屬性目前尚不可用。您可以在Azure AD B2C UserVoice forum中申請此功能。
在這個時候,你有兩種選擇:
- 強制用戶通過選擇中明確提供此值它作爲你的政策的註冊屬性。
- 添加一些代碼,用任何您想要的邏輯(例如,在處理新註冊的控制器中或通過定期運行的無頭客戶端)更新這些屬性。
下面是Net代碼快速&骯髒的片段,你可以使用這個(假設你想這樣做的權威性管道(Startup.Auth.cs):
private async Task OnSecurityTokenValidated(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
{
try
{
var userObjectId = notification.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
// You'll need to register a separate app for this.
// This app will need APPLICATION (not Delegated) Directory.Read permissions
// Check out this link for more info:
// https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-devquickstarts-graph-dotnet
var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(string.Format(graphAuthority, tenant));
var t = await authContext.AcquireTokenAsync(graphResource, new ClientCredential(graphClientId, graphClientSecret));
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + t.AccessToken);
var url = graphResource + tenant + "https://stackoverflow.com/users/" + userObjectId + "/?api-version=1.6";
var name = "myDisplayName";
var content = new StringContent("{ \"displayName\":\"" + name + "\" }", Encoding.UTF8, "application/json");
var result = await client.PostAsync(url, content);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
你」當您設置您的OpenIdConnectAuthenticationOptions像這樣LL引用此方法:
new OpenIdConnectAuthenticationOptions
{
// (...)
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
SecurityTokenValidated = OnSecurityTokenValidated,
},
// (...)
};
我認爲這裏的實際問題是,我是混爲一談的用戶名要求與顯示名稱我想我真正想要的是獲取用戶名後面,但塔t似乎不是註冊/策略中的一個選項。 – SWC