title | description | author | manager | ms.author | ms.date | ms.reviewer | ms.service | ms.topic |
---|---|---|---|---|---|---|---|---|
Code samples for authentication and authorization | An index of identity platform code samples, grouped by app types, languages, and frameworks, shows how these libraries enable app authentication and authorization. | OwenRichards1 | CelesteDG | owenrichards | 01/27/2025 | jmprieur | identity-platform | sample |
These code samples are built and maintained by Microsoft to demonstrate usage of our authentication libraries with the Microsoft identity platform. Common authentication and authorization scenarios are implemented in several application types, development languages, and frameworks.
- Sign in users to web applications and provide authorized access to protected web APIs.
- Protect a web API by requiring an access token to perform API operations.
Each code sample includes a README.md file describing how to build the project (if applicable) and run the sample application. Comments in the code help you understand how these libraries are used in the application to perform authentication and authorization by using the identity platform.
Use the tabs to sort the samples by application type, or your preferred language/framework.
These samples show how to write a single-page application secured with Microsoft identity platform. These samples use one of the flavors of MSAL.js.
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial React • Sign in users MSAL React Authorization code with PKCE Quickstart Tutorial Angular • Sign in users MSAL Angular Authorization code with PKCE Quickstart Tutorial JavaScript • Sign in users
• Call Microsoft Graph
• Call Node.js web API
• Deploy to Azure Storage and App ServiceMSAL.js Authorization code with PKCE Quickstart Blazor WebAssembly • Sign in users
• Call Microsoft Graph
• Deploy to Azure App ServiceMSAL.js Authorization code with PKCE Quickstart
The following samples illustrate web applications that sign in users. Some samples also demonstrate the application calling Microsoft Graph, or your own web API with the user's identity.
The following samples show how to protect a web API with the Microsoft identity platform, and how to call a downstream API from the web API.
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial ASP.NET • Call Microsoft Graph MSAL.NET On-Behalf-Of (OBO) Quickstart ASP.NET Core • Access control (protected routes) with the Microsoft identity platform MSAL.NET On-Behalf-Of (OBO) Quickstart Tutorial Java • Protect your Java Spring Boot web API with the Microsoft identity platform MSAL Java On-Behalf-Of (OBO) Node.js • Protect a Node.js web API MSAL Node Authorization bearer
The following samples show public client desktop applications that access the Microsoft Graph API, or your own web API in the name of the user. Apart from the Desktop (Console) with Web Authentication Manager (WAM) sample, all these client applications use the Microsoft Authentication Library (MSAL).
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial .NET Core • Call Microsoft Graph
• Call Microsoft Graph with token cache
• Call Microsoft Graph with custom web UI HTML
• Call Microsoft Graph with custom web browser
• Sign in users with device code flow
• Call Microsoft Graph by signing in users using username/passwordMSAL.NET • Authorization code with PKCE
• Device code
• Resource owner password credentialsJava • Call Microsoft Graph MSAL Java Integrated Windows authentication Node.js • Sign in users MSAL Node Authorization code with PKCE Quickstart Tutorial Python • Sign in users MSAL Python Resource owner password credentials Windows Presentation Foundation (WPF) • Sign in users and call Microsoft Graph
• Windows Presentation Foundation (WPF) user sign-in, protected web API access (Microsoft Graph)
• Sign in users and call ASP.NET Core web API
• Sign in users and call Microsoft GraphMSAL.NET Authorization code with PKCE Quickstart Tutorial
The following samples show public client mobile applications that access the Microsoft Graph API. These client applications use the Microsoft Authentication Library (MSAL).
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial .NET Core • Call Microsoft Graph using MAUI
• Call Microsoft Graph using MAUI with brokerMSAL.NET Authorization code with PKCE iOS • Call Microsoft Graph native MSAL iOS Authorization code with PKCE Quickstart Tutorial Java • Sign in users and call Microsoft Graph MSAL Android Authorization code with PKCE Quickstart Tutorial Kotlin • Sign in users and call Microsoft Graph MSAL Android Authorization code with PKCE
The following samples show an application that accesses the Microsoft Graph API with its own identity (with no user).
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial .NET • .NET console app that accesses a protected web API
• Multitenant with Microsoft identity platform endpointMSAL.NET Client credentials grant Quickstart Tutorial .NET Core • Call Microsoft Graph
• Call web API
• Using managed identity to call MSGraph
• Using managed identity to call an API
• Worker role calling an APIMicrosoft.Identity.Web Client credentials grant Java • Call Microsoft Graph with Secret
• Call Microsoft Graph with CertificateMSAL Java Client credentials grant Quickstart Node.js • Call Microsoft Graph with secret MSAL Node Client credentials grant Quickstart Tutorial Python • Call Microsoft Graph with secret
• Call Microsoft Graph with certificateMSAL Python Client credentials grant Quickstart
The following sample shows a public client application running on a device without a web browser. The app can be a command-line tool, an app running on Linux or Mac, or an IoT application. The sample features an app accessing the Microsoft Graph API, in the name of a user who signs in interactively on another device (such as a mobile phone). This client application uses the Microsoft Authentication Library (MSAL).
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial .NET Core • Invoke protected API from text-only device MSAL.NET Device code Java • Sign in users and invoke protected API from text-only device MSAL Java Device code Python • Call Microsoft Graph MSAL Python Device code
The following samples show how to protect an Azure Function using HttpTrigger and exposing a web API with the Microsoft identity platform, and how to call a downstream API from the web API.
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Python • Python Azure function web API secured by Microsoft Entra ID MSAL Python Authorization code
The following sample illustrates Microsoft Teams Tab application that signs in users. Additionally it demonstrates how to call Microsoft Graph API with the user's identity using the Microsoft Authentication Library (MSAL).
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Node.js • Teams Tab app: single sign-on (SSO) and call Microsoft Graph MSAL Node On-Behalf-Of (OBO)
The following samples show how to configure your application to accept sign-ins from any Microsoft Entra tenant. Configuring your application to be multitenant means that you can offer a Software as a Service (SaaS) application to many organizations, allowing their users to be able to sign-in to your application after providing consent.
[!div class="mx-tdCol2BreakAll"]
Language /
PlatformCode sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial ASP.NET Core • ASP.NET Core MVC web application calls Microsoft Graph API
• ASP.NET Core MVC web application calls ASP.NET Core web APIMSAL.NET • OpenID connect
• Authorization code
The following samples show how to build applications using the C# language and frameworks
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Desktop • Call Microsoft Graph
• Call Microsoft Graph with token cache
• Call Microsoft Graph with custom web UI HTML
• Call Microsoft Graph with custom web browser
• Sign in users with device code flow
• Call Microsoft Graph by signing in users using username/passwordMSAL.NET • Authorization code with PKCE
• Device codeMobile • Call Microsoft Graph using MAUI
• Call Microsoft Graph using MAUI with brokerMSAL.NET Authorization code with PKCE Service/daemon • Call Microsoft Graph
• Call web API
• Using managed identity and Azure key vaultMSAL.NET Client credentials grant Headless • Invoke protected API from text-only device MSAL.NET Device code
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web application • Microsoft Graph Training Sample
• Sign in users and call Microsoft Graph with admin restricted scopeMSAL.NET • OpenID connect
• Authorization codeQuickstart Web API • Call Microsoft Graph MSAL.NET On-Behalf-Of (OBO) Service/
daemon• Multitenant with Microsoft identity platform endpoint MSAL.NET Client credentials grant
[!div class="mx-tdCol2BreakAll"]
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Single-page application • Sign in users
• Call Microsoft Graph
• Deploy to Azure App ServiceMSAL.js Implicit Flow Quickstart Web application • Sign in users
• Call Microsoft Graph
• Call web APIMSAL.NET Implicit/Hybrid flow
The following samples show how to build applications for the iOS platform.
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Mobile • Call Microsoft Graph native MSAL iOS Authorization code with PKCE Quickstart Tutorial
The following samples show how to build applications for the JavaScript language and platform.
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Single-page application • Sign in users
• Call Microsoft Graph
• Call Node.js web API
• Deploy to Azure Storage and App ServiceMSAL.js Authorization code with PKCE Quickstart
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Single-page application • Sign in users MSAL Angular Authorization code with PKCE Quickstart Tutorial
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web API • Protect a Node.js web API MSAL Node Authorization bearer Desktop • Sign in users MSAL Node Authorization code with PKCE Tutorial Service, daemon • Call Microsoft Graph with secret MSAL Node Client credentials grant Quickstart Microsoft Teams applications • Teams Tab app: single sign-on (SSO) and call Microsoft Graph MSAL Node On-Behalf-Of (OBO)
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web application • Sign in users
• Call Microsoft Graph
• Deploy to Azure App Service
• Use App Roles for access control
• Use Security Groups for access control
• Web app that sign in usersMSAL Node Authorization code Quickstart Tutorial
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Single-page application • Sign in users MSAL React • Authorization code with PKCE Quickstart Tutorial
The following samples show how to build applications for the Java language and platform.
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web API • Sign in users MSAL Java On-Behalf-Of (OBO) Desktop • Call Microsoft Graph MSAL Java Integrated Windows authentication Mobile • Sign in users and call Microsoft Graph MSAL Android Authorization code with PKCE Service/
daemon• Call Microsoft Graph with Secret
• Call Microsoft Graph with CertificateMSAL Java Client credentials grant Quickstart
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web application Microsoft Entra Spring Boot Starter Series
• Sign in users
• Call Microsoft Graph
• Use App Roles for access control
• Use Groups for access control
• Deploy to Azure App Service
• Protect a web API• MSAL Java
• Microsoft Entra ID Boot StarterAuthorization code Tutorial
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web application Spring-less Servlet Series
• Sign in users
• Call Microsoft Graph
• Use App Roles for access control
• Use Security Groups for access control
• Deploy to Azure App ServiceMSAL Java Authorization code
The following samples show how to build applications for the Python language and platform.
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Azure Functions as web APIs • Python Azure function web API secured by Microsoft Entra ID MSAL Python Authorization code Desktop • Sign in users MSAL Python Resource owner password credentials Headless • Call Microsoft Graph MSAL Python Device code Daemon • Call Microsoft Graph with secret
• Call Microsoft Graph with certificateMSAL Python Client credentials grant Quickstart
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web application • Sign in users
• A template to sign in Microsoft Entra ID, and optionally call a downstream API (Microsoft Graph)MSAL Python Authorization code Quickstart Tutorial
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web application • Sign in users
• Integrating Microsoft Entra ID with a Python web application written in DjangoMSAL Python Authorization code
The following samples show how to build applications with Kotlin.
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Mobile • Sign in users and call Microsoft Graph MSAL Android Authorization code with PKCE
The following samples show how to build applications with Ruby.
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Web application Graph Training
• Sign in users and call Microsoft GraphOmniAuth OAuth2 Authorization code
The following samples show how to build applications with Windows Presentation Foundation (WPF).
[!div class="mx-tdCol2BreakAll"]
App type Code sample(s)
on GitHubAuth
librariesAuth flow Quickstart Tutorial Desktop • Sign in users and call Microsoft Graph MSAL.NET Authorization code with PKCE Desktop • Sign in users and call ASP.NET Core web API
• Sign in users and call Microsoft GraphMSAL.NET Authorization code with PKCE Quickstart Tutorial
If you'd like to delve deeper into more sample code, see: