VB.Net UserPrincipal is Nothing if using Azure AD

658 views Asked by At

We have a winform application and in order to login, we are trying to get the currently logged in user using UserPrincipal.Current

When the currently logged in user is an Azure AD account, the UserPrincipal is Nothing.

  • Is there a way to get the currently logged in user when using Azure AD ?

Code used:

Dim currentWinUser As UserPrincipal = UserPrincipal.Current
2

There are 2 answers

3
Allen Wu On

Please use Microsoft Graph API to get the logged in user

See reference here: Get a user.

GET https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}

You need an access token to call Microsoft Graph API. See Authentication and authorization basics for Microsoft Graph here.

A sample may be helpful here.

0
Marilee Turscak - MSFT On

You can also get it using the Azure CLI:

az ad signed-in-user show --query objectId -o tsv

As Alan mentioned Graph API is a good way to go though.

User.Identity.Name will also give you the info for the logged in on-prem Active Directory user.