Get all user properties from microsoft graph

2020-02-13 13:54发布

问题:

We have an application wich has used a local AD to fetch user info. Some customers want to move to the cloud and are using Azure AD. We extended the app to sign users in via owin and now we're fetching users via Microsoft Graph.

However from Microsoft Graph we do not get full user profiles. We want to fetch all properties on users, not just the basic ones.

var client = new RestClient(string.Format("https://graph.microsoft.com/v1.0/users/{0}", userEmail));
request = new RestRequest();
request.Method = Method.GET;
request.AddHeader("Authorization", _token.Token);
var reponse = client.Execute(request);

This only gives me some information though, for example I dont get 'Department' from this. Is it possible to configure in azure what should be returned here, if so then where? Or do I need something other than /users/?

Different customers might have different special properties that need to be fetched. So the best solution would be to have an endpoint to call and get everything, including special properties not standard in azure ad. After that i can parse it on my side. Is this possible?

The app har permission to read both basic and full profiles. Do I need something more?

回答1:

That's the normal behaviour of Graph API, see documentation here and this extract:

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

You have to specify all fields in the select, as $select=* will only output the key fields in Graph API implementation.

So you will not be able to get what you ask (variable custom fields).

More info on the fields of User can be found here



回答2:

As already stated by NicolasR, you must list all the fields you want to retrieve by using the "$select" parameter; if you want, instead, to retrieve the custom fields, you can either add them to the previous parameter (if you know their names) or you can use "$expand=extensions"