How to consume Microsoft Graph API using Azure AD authentication in .NET Core

David Bottiau
2 min readJul 18, 2019

--

Following this article, you are now able to authenticate your users in your application using Azure Active Directory. But you can only retrieve basic informations about a user (id, mail, name). That can be enough for most applications but some business applications may need to use other data inside the Microsoft ecosystem.

Hopefully, there is an API for that and its name is Microsoft Graph, and you can consume it by reusing the access token provided by the Azure AD authentication mechanism.

Configuration

Azure AD configuration

At the moment, you should already have an Azure AD application. But in order to consume the Graph API, you will have to update the configuration. So, let’s go back in the Azure portal and configure our app registration.

First of all, you have to authorize access to the Microsoft Graph through the Azure AD application:

Azure AD API permissions

On every application, the User.Read permission is required in order to login the current user and retrieve its information. You can also add other permissions based on your application needs.

Then, during the authentication process to MS Graph in the back, we need to provide a ClientSecret key.

Azure AD Client Secret

Backend configuration

From your Azure AD app, you can now set your configuration file:

Before accessing the Microsoft Graph, we need to set a Microsoft Graph token. You can the process below.

Making API requests

Now, that you are authenticated with both the Azure AD token and the Microsoft Graph token, you can query this API now.

If you want to see the code in details, please check the following repository: https://github.com/Odonno/azuread-msgraph-dotnet-core-example

Get current user

The one thing you can do is to retrieve detailed information about the user currently logged in.

Get a list of users

Also, on interesting feature could be to search users inside your organization. There is not really a search function in the Microsoft Graph API but you can make something close by using the ODATA operators $top and $filter.

Thank you!

This article is mainly inspired by the one made by Joonas Westlin https://joonasw.net/view/azure-ad-on-behalf-of-aspnet-core

--

--

David Bottiau

Software designer, open-minded, Lean being and UX advocate.