3
votes

I created an Azure Active Directory Application and applied the code from the following tutorial to enable login: https://azure.microsoft.com/en-us/resources/samples/active-directory-dotnet-webapp-openidconnect-aspnetcore/

After login the following returns my email adress:

Console.WriteLine(User.Identity.Name)

How would i retreive the First and Last Name of this user?

2
Have you tried using Graph API? - Prasanth
OpenId scope returns first name and last name claims in token. Should be able to extract from claims principal after authN - Mardoxx

2 Answers

7
votes

Found it:

var fn = User.FindFirst(ClaimTypes.GivenName).Value;
var ln = User.FindFirst(ClaimTypes.Surname).Value;
System.Console.WriteLine("me :" + fn + " " + ln );
1
votes

I use this, this gets you the first and last name together -

var name = claims.FirstOrDefault(c => c.Type == "name")?.Value;