A .NET 9.0 console application demonstrating OAuth 2.0 authentication with Microsoft Identity Platform and Microsoft Graph API integration.
If you'd like to learn more, I wrote an accompanying article on my blog.
This application demonstrates how to:
- Authenticate using OAuth 2.0 Client Credentials flow
- Acquire access tokens using Microsoft Identity Client (MSAL)
- Make authenticated requests to Microsoft Graph API
- Retrieve user data from Microsoft Graph
- .NET 9.0 SDK
- An Azure Active Directory (Azure AD) application registration
- Client ID, Tenant ID, and Client Secret from your Azure AD app
- Navigate to Azure Portal
- Go to Azure Active Directory > App registrations > New registration
- Register your application and note the following:
- Application (client) ID
- Directory (tenant) ID
- Under Certificates & secrets, create a new client secret and save the value
- Under API permissions, add the following permissions:
- Microsoft Graph > Application permissions >
User.Read.All - Grant admin consent for your organization
- Microsoft Graph > Application permissions >
-
Clone the repository:
git clone <repository-url> cd oauthdemo
-
Update the
appsettings.jsonfile with your Azure AD credentials:{ "OAuth": { "ClientId": "your-client-id", "TenantId": "your-tenant-id", "Authority": "https://login.microsoftonline.com/your-tenant-id", "Scopes": [ "https://graph.microsoft.com/.default" ], "ClientSecret": "your-client-secret" } }
Restore the NuGet packages:
dotnet restoreRun the application:
dotnet run --project oauthdemoThe application will:
- Load configuration from
appsettings.json - Authenticate with Microsoft Identity Platform
- Acquire an access token
- Call the Microsoft Graph API to retrieve users
- Display the results in the console
- Microsoft.Identity.Client (v4.78.0) - Microsoft Authentication Library for .NET
- Microsoft.Extensions.Configuration (v9.0.10) - Configuration framework
- Microsoft.Extensions.Configuration.Json (v9.0.10) - JSON configuration provider
- Microsoft.Extensions.Configuration.Binder (v9.0.10) - Configuration binding
oauthdemo/
├── oauthdemo.sln # Solution file
├── oauthdemo/
│ ├── Program.cs # Main application entry point
│ ├── appsettings.json # Configuration file
│ └── oauthdemo.csproj # Project file
└── README.md # This file
- Authentication Failed: Verify your Client ID, Tenant ID, and Client Secret are correct
- Insufficient Permissions: Ensure admin consent has been granted for the required API permissions
- Configuration Error: Check that
appsettings.jsonis being copied to the output directory
This project is provided as a demonstration and learning resource.
Feel free to submit issues or pull requests for improvements.