I'm using standart snippet from MS library to get users and their properties from my azure AD . But unfortunately I found that this snippet doesn't get all properties that users have and only get their display name, fullname, surname, thats all, other properties are null
, but I need to get all properties that users have. Im already google this question a lot and only found solution for LDAP that does not fit for me.
Image of JSON output of user properties:
[{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},{"Properties":{},"Id":null,"Display":null,"Department":null,"GivenName":"Example","Surname":"Example","UserPrincipalName":"Example@Example.com"},
So question is, how to get all user properties from Azure AD using C#?
My user model:
public async Task<List<ResultsItem>> GetUsers(GraphServiceClient graphClient)
{
List<ResultsItem> items = new List<ResultsItem>();
// Get users.
IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().GetAsync();
// Populate the view model.
if (users?.Count > 0)
{
foreach (User user in users)
{
// Filter out conference rooms.
string displayName = user.DisplayName ?? "";
if (!displayName.StartsWith("Conf Room"))
{
// Get user properties.
items.Add(new ResultsItem
{
Mail = user.Mail,
Display = user.DisplayName,
Id = user.Id,
Department = user.Department,
GivenName = user.GivenName,
Surname = user.Surname,
UserPrincipalName = user.UserPrincipalName
});
}
}
}
var jsonEmployees = JsonConvert.SerializeObject(items); // Convert to JSON
return items;
}
According to your description, I registered the AD v2.0 app and used MSAL for retrieving the access token by using the User.Read and User.ReadBasic.All scopes. Use fiddler to capture the network traces when getting users, I could get the response as follows:
As Microsoft Graph List users states as follows:
By default, only a limited set of properties are returned ( businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, userPrincipalName ).
To return an alternative property set, you must specify the desired set of user properties using the OData
$select
query parameter. For example, to return displayName, givenName, and postalCode, you would use the add the following to your query$select=displayName,givenName,postalCode
For a simple way, I just use the Microsoft Graph Explorer and access https://graph.microsoft.com/v1.0/users?$select=Department,Mail,Display,Id,GivenName,Surname,UserPrincipleName
as follows:
When using the client library, you could use the following code:
var users = await graphClient.Users.Request().Select("Department,Mail,Display,Id,GivenName,Surname,UserPrincipleName").GetAsync();
Details you could follow the Query options section under Microsoft Graph .NET Client Library Overview .
Moreover, you need to check if there has any more records and retrieve all users via the following code snippet:
var userList = new List<User>();
IGraphServiceUsersCollectionPage pagedCollection = await graphClient.Users.Request().Select("Department,Mail,Display,Id,GivenName,Surname,UserPrincipleName").GetAsync();
if (pagedCollection != null)
{
do
{
List<User> usersList = pagedCollection.CurrentPage.ToList();
foreach (var user in usersList)
{
userList.Add(user);
}
pagedCollection = await pagedCollection.NextPageRequest.GetAsync();
} while (pagedCollection != null);
}
UPDATE:
I leveraged the Microsoft Graph Snippets Sample for ASP.NET 4.6 sample and registered my AD V2.0 app and replaced the ida:AppId
and ida:AppSecret
setting under my web.config file. It could work on my side as follows:
Using fiddler, I could capture the network trace for https://graph.microsoft.com/v1.0/users
and it could correctly return the user properties. I would recommend you debug your application and check the result after invoked await graphClient.Users.Request().GetAsync();
, also check the properties of the user
instance when you iterate the users
collection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.