Skip to content
deanhumePublic

About

A simple OAuth 2 demo

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

OAuth Demo

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.

Overview

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

Prerequisites

  • .NET 9.0 SDK
  • An Azure Active Directory (Azure AD) application registration
  • Client ID, Tenant ID, and Client Secret from your Azure AD app

Azure AD Application Setup

  1. Navigate to Azure Portal
  2. Go to Azure Active Directory > App registrations > New registration
  3. Register your application and note the following:
    • Application (client) ID
    • Directory (tenant) ID
  4. Under Certificates & secrets, create a new client secret and save the value
  5. Under API permissions, add the following permissions:
    • Microsoft Graph > Application permissions > User.Read.All
    • Grant admin consent for your organization

Configuration

  1. Clone the repository:

    git clone <repository-url>
    cd oauthdemo
  2. Update the appsettings.json file 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"
      }
    }

Installation

Restore the NuGet packages:

dotnet restore

Usage

Run the application:

dotnet run --project oauthdemo

The application will:

  1. Load configuration from appsettings.json
  2. Authenticate with Microsoft Identity Platform
  3. Acquire an access token
  4. Call the Microsoft Graph API to retrieve users
  5. Display the results in the console

Dependencies

  • 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

Project Structure

oauthdemo/
├── oauthdemo.sln           # Solution file
├── oauthdemo/
│   ├── Program.cs          # Main application entry point
│   ├── appsettings.json    # Configuration file
│   └── oauthdemo.csproj    # Project file
└── README.md              # This file

Troubleshooting

Common Issues

  1. Authentication Failed: Verify your Client ID, Tenant ID, and Client Secret are correct
  2. Insufficient Permissions: Ensure admin consent has been granted for the required API permissions
  3. Configuration Error: Check that appsettings.json is being copied to the output directory

License

This project is provided as a demonstration and learning resource.

Contributing

Feel free to submit issues or pull requests for improvements.

About

A simple OAuth 2 demo

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages