残念ながら、ユーザー名は、トークンに伝承するための請求などの選択のためにまだ利用できません。 Include username in JWT claims
今回のようにあなたの唯一のオプションはグラフを経由して、それを自分で取得することです:あなたはそれを優先順位付けを支援するためのAzure AD B2Cユーザーボイスフォーラムで聞いて、このために投票する必要があります。ここで
は、あなたがこのために使用できる.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