Get user name of azure ad user

Nadav Spinco 0 Reputation points
2024-08-24T14:16:25.93+00:00

Hi,

I am having trouble retrieving the username of a pure Azure AD user (not hybrid) using the Microsoft Graph API.

On the client side, when I run the following C# code:

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

the userName value is formatted as AzureAd\NadavSpinco. However, I am unable to find an exact match for NadavSpinco in any of the attributes returned by the Graph API. The DisplayName obtained from the API is Nadav Spinco.

Is it correct to assume that the username corresponds to the DisplayName attribute without spaces? Or is there another way to retrieve the username from the Graph API?

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2024-08-24T16:24:12.0566667+00:00

    No it is not correct that the display name and user name are related in any way. A username is created when the account is created. The display name can be changed at any time and reflects what the person wants to be called.

    Providing us a sample of what you have coded would greatly increase the odds of us finding an issue in your code. In general the user's account information is stored in the identities collection, of which there can be several. By default this collection isn't returned so you need to ask for it in your $select query. See here.

    Within the identity object itself you'll find the user's name, if they have one. This is ultimately what the Name property on an identity returns back. It bubbles it up so you don't have to go through the process of navigating the identities.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.