Microsoft Graph – How to implement IAuthenticationProvider

Microsoft Graph – How to implement IAuthenticationProvider

Beginning

In the article about integration Azure AD with Angular I got a question how to implement Microsoft Graph on API side. I’ll show you here how to implement IAuthenticationProvider in the simplest way.

Overview

The Microsoft Graph is powerful tool to interact with most important Office 365 and Azure Active Directory. List of most common services what you can access data:

  • Azure Active Directory
  • Office 365
    • OneDrive
    • Outlook / Exchange
    • OneNote
    • Planner
    • Microsoft Teams
    • SharePoint
  • Windows 10 services

Register & configure application on azure

After login on https://portal.azure.com go to Azure Active Directory > App Registrations and click New Registration and follow simple instructions, if you will stuck do this by analogy from this article.

If you have already registered application, you need generate your secret for application, you can do this in Certificates & secrets section while editing registered application.

certificates_secrets_azure
certificates_secrets_azure

Important! Copy immediately your secret, generated secret is only once available to see.

In next step you should give application appropriate permissions in API permissions section. After that we can go to the implementation in C#

permissions_azure
permissions_azure

Implementation

Now you need to install these Nuget packages:

  • Microsoft.Graph
  • Microsoft.Graph.Core
  • Microsoft.Identity.Client

Now it’s time to implement IAuthenticationProvider, I’ll show you the simplest way to implement it in my opinion. However, there are many different ways to do it.

AuthenticationProvider.cs

Summary

That’s all! After that simple implementation, you need to create only GraphServiceClient and inject in constructor your implementation of IAuthenticationProvider and make request 🙂

sample_request-1
sample_request-1

If my articles helped you, that would be nice if you like my Facebook fanpage 🙂