3
我已配置身份提供者以使用带有用户名而不是密码的本地帐户。Azure AD B2C - 使用“用户名”不提供用户名声明的本地IDP
我创建了一个显示名称,用户名和电子邮件的用户。
我可以选择“显示名称”和“电子邮件地址”所有政策申请要求,但“用户名”是不是一种选择。我还确认,用户名称声明未提供给我的应用程序。
如何配置Azure AD B2C以使其提供用户名声明?
我已配置身份提供者以使用带有用户名而不是密码的本地帐户。Azure AD B2C - 使用“用户名”不提供用户名声明的本地IDP
我创建了一个显示名称,用户名和电子邮件的用户。
我可以选择“显示名称”和“电子邮件地址”所有政策申请要求,但“用户名”是不是一种选择。我还确认,用户名称声明未提供给我的应用程序。
如何配置Azure AD B2C以使其提供用户名声明?
不幸的是,用户名还没有提供可供选择的要求继续传承令牌。你应该投票支持这个要求在Azure的AD B2C UserVoice的论坛,以帮助其优先级:因为这个时间是通过图形自行取回Include username in JWT claims
你唯一的选择。
下面是Net代码一个快速&肮脏的片段,您可以使用此:
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 + "https://stackoverflow.com/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());
}
}
你会引用此方法,当你设置你的OpenIdConnectAuthenticationOptions像这样:
new OpenIdConnectAuthenticationOptions
{
// (...)
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
SecurityTokenValidated = OnSecurityTokenValidated,
},
// (...)
};
以上看起来是一个很好的建议。这看起来很明显,但对于那些不熟悉Azure B2C的人,不要忘记,为了让Azure B2C将ObjectId作为标记中的声明返回(http://schemas.microsoft.com/identity/claims/objectidentifier) ,您需要确保您在上面显示的屏幕中检查了用户对象ID,以便您可以选择要将哪些属性包含在声明中。 –
我已经签署了这个请求,因为我觉得很奇怪,B2C在那里验证一个用户,但是并没有告诉我们哪个用户...... – radders