我已将身份提供程序配置为使用具有用户名而非密码的本地帐户。
我创建了一个具有显示名称,用户名和电子邮件的用户。
我可以选择"显示名称"和"电子邮件地址"所有政策的申请要求,但"用户名"不是一种选择。我还确认没有向我的应用程序提供用户名声明。
如何配置Azure AD B2C,使其提供用户名声明?
答案 0 :(得分:2)
不幸的是,用户名尚未被选中作为在令牌中传递的声明。您应该在Azure AD B2C UserVoice论坛中为此问题投票,以帮助确定其优先级:Include username in JWT claims
您唯一的选择是通过图表自行检索。
这里有一个快速&您可以使用的.Net代码的脏片段:
i0118
在设置OpenIdConnectAuthenticationOptions时,您将引用此方法,如下所示:
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));
string result;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + t.AccessToken);
var url = graphResource + tenant + "/users/" + userObjectId + "/?api-version=1.6";
result = await client.GetStringAsync(url);
}
var jsonResult = JObject.Parse(result);
var username = jsonResult["signInNames"].FirstOrDefault(j => j["type"].ToString() == "userName")?["value"]?.ToString();
notification.AuthenticationTicket.Identity.AddClaim(new Claim("username", username));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}