Introduction
In this blog post, I will explore with you some of the complex authentication scenarios that developers may encounter when using Microsoft Graph and how to implement them using the Microsoft Graph SDKs and the Microsoft Authentication Library (MSAL) for various platforms. I will cover the following topics:
- What is MSAL, and how does it work with Microsoft Graph?
- How do you use MSAL to acquire tokens for Microsoft Graph?
- How do you handle conditional access policies and multi-factor authentication?
- How can MSAL be used to implement single sign-on (SSO) and single sign-out (SSO) across multiple applications?
- How do we use MSAL to implement device code flow and integrated Windows authentication?
- How do we use MSAL to implement on-behalf-of flow and client credentials flow?
What is MSAL, and how does it work with Microsoft Graph?
MSAL is a set of libraries that enable developers to acquire tokens from the Microsoft identity platform endpoint in order to access secured resources such as Microsoft Graph. MSAL abstracts the complexity of authentication protocols and provides a consistent interface across different platforms and languages. MSAL supports various authentication flows, such as interactive, silent, device code, integrated Windows authentication, on-behalf-of, and client credentials.
MSAL works with Microsoft Graph by leveraging the OAuth 2.0 and OpenID Connect protocols, which are the industry standards for securing web APIs and web applications. MSAL helps developers to obtain access tokens and refresh tokens from the Microsoft identity platform endpoint, and use them to call Microsoft Graph API endpoints. MSAL also handles token caching, token renewal, and token validation.
MSAL is available for various platforms and languages, such as .NET, Java, Python, and JavaScript. MSAL is also integrated with the Microsoft Graph SDKs, which are wrappers around the Microsoft Graph API that provide a fluent and easy-to-use interface for developers. The Microsoft Graph SDKs handle the authentication process by using MSAL internally and allow developers to focus on the business logic of their applications.
How do you use MSAL to acquire tokens for Microsoft Graph?
The basic steps to use MSAL to acquire tokens for Microsoft Graph are as follows:
- Create an application registration in the Azure portal and configure the required permissions and settings for Microsoft Graph. You can follow the Microsoft article to learn how to register the app in the Azure portal. https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app.
- Install the MSAL library and the Microsoft Graph SDK for your platform and language of choice.
- Initialize an MSAL client application object with the application ID, authority, and redirect URI of your application registration.
- Use the MSAL client application object to acquire tokens for Microsoft Graph, either interactively or silently, depending on the authentication flow and the user context.
- Use the access token to call the Microsoft Graph SDK methods or directly call the Microsoft Graph API endpoints.
.NET
// Install the Microsoft.Identity.Client and Microsoft.Graph NuGet packages
using Microsoft.Identity.Client;
using Microsoft.Graph;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Create an MSAL client application object
var app = PublicClientApplicationBuilder.Create("your-application-id")
.WithAuthority("https://login.microsoftonline.com/your-tenant-id")
.WithRedirectUri("https://localhost")
.Build();
// Acquire an access token interactively
var scopes = new[] { "user.read", "mail.read" };
var result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
// Use the access token to call the Microsoft Graph SDK
var graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(request =>
{
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
return Task.CompletedTask;
}));
var user = await graphClient.Me.Request().GetAsync();
var messages = await graphClient.Me.MailFolders.Inbox.Messages.Request().GetAsync();
}
}
Java
// Install the msal4j and microsoft-graph libraries
import com.microsoft.aad.msal4j.*;
import com.microsoft.graph.authentication.*;
import com.microsoft.graph.models.*;
import com.microsoft.graph.requests.*;
import java.net.URI;
import java.util.Collections;
import java.util.Set;
public class GraphExample {
public static void main(String[] args) throws Exception {
// Create an MSAL client application object
PublicClientApplication app = PublicClientApplication.builder("your-application-id")
.authority("https://login.microsoftonline.com/your-tenant-id")
.build();
// Acquire an access token interactively
Set<String> scopes = Collections.singleton("user.read mail.read");
InteractiveBrowserProviderParams params = InteractiveBrowserProviderParams
.builder(new URI("http://localhost"))
.scopes(scopes)
.build();
IAuthenticationResult result = app.acquireToken(params).get();
// Use the access token to call the Microsoft Graph SDK
IAuthenticationProvider authProvider = new TokenCredentialAuthProvider(scopes,
new StaticTokenCredential(result.accessToken()));
GraphServiceClient<IUser> graphClient = GraphServiceClient
.<IUser>builder()
.authenticationProvider(authProvider)
.buildClient();
User user = graphClient.me().buildRequest().get();
IMailFolderCollectionPage messages = graphClient.me().mailFolders("Inbox").messages()
.buildRequest().get();
}
}

Join the conversation! Your thoughts help the community grow.