This guide uses client credentials authentication for simplicity. For production applications with user interaction, see the authentication guide for other authentication flows.
What You’ll Build
By the end of this guide, you’ll have a working console application that:- Authenticates to Microsoft Graph using client credentials
- Creates a GraphServiceClient instance
- Retrieves user information from your Azure AD tenant
- Handles errors gracefully
Register Your Application in Azure Portal
Before you can call Microsoft Graph, you need to register your application in Azure Active Directory.
Navigate to Azure Portal
- Go to the Microsoft Application Registration Portal
- Sign in with your Microsoft 365 account (work or school account)
Create a New Registration
- Click New registration
- Enter an application name (e.g., “My Graph App”)
- Select Accounts in this organizational directory only for the supported account types
- Leave the Redirect URI blank for now
- Click Register
Save Your Application Details
After registration, you’ll see the application overview page. Save these values:- Application (client) ID - You’ll need this for authentication
- Directory (tenant) ID - You’ll need this for authentication
Create a Client Secret
- In the left menu, click Certificates & secrets
- Click New client secret
- Enter a description (e.g., “App Secret”)
- Select an expiration period (6 months, 12 months, or 24 months)
- Click Add
- Important: Copy the secret Value immediately - you won’t be able to see it again!
Grant API Permissions
- In the left menu, click API permissions
- Click Add a permission
- Select Microsoft Graph
- Select Application permissions (not Delegated permissions)
- Search for and select User.Read.All
- Click Add permissions
- Click Grant admin consent for [Your Organization] (requires admin rights)
- Confirm by clicking Yes
Application permissions allow your app to access data without a signed-in user. This is perfect for background services and automation. For interactive apps, use Delegated permissions instead.
Configure Authentication
Now configure your application to authenticate with Microsoft Graph using the credentials from Step 1.Linux/macOS:
Store Configuration Securely
Create anappsettings.json file (add to .gitignore):Alternative: Use Environment Variables
Set environment variables (recommended for production):Windows (PowerShell):Create GraphServiceClient
Update
Program.cs to create and configure the GraphServiceClient:Understanding the Code
Let’s break down what’s happening:- ClientSecretCredential - Creates an authentication provider using client credentials flow
- GraphServiceClient - The main entry point for all Microsoft Graph operations
- The credential handles token acquisition automatically when you make API calls
The SDK automatically uses the scope
https://graph.microsoft.com/.default for application permissions, so you don’t need to specify scopes explicitly.Complete Working Example
Here’s the complete, production-ready code with error handling and best practices:Understanding Query Parameters
The SDK provides a fluent API for adding query parameters:Common API Calls
Here are more examples of common operations:Get Current User (Delegated Permissions)
Get User’s Messages
Get User’s Calendar Events
Get User’s OneDrive Files
Troubleshooting
Authentication Failed
Error:401 Unauthorized or AADSTS700016: Application not found
Solution:
- Verify your tenant ID, client ID, and client secret are correct
- Ensure you granted admin consent for the required permissions
- Check that the application is enabled in Azure AD
Insufficient Privileges
Error:403 Forbidden or Insufficient privileges to complete the operation
Solution:
- Verify you granted the correct API permissions (e.g., User.Read.All)
- Ensure you clicked “Grant admin consent” in the Azure Portal
- Wait a few minutes for permissions to propagate
User Not Found
Error:404 Not Found or Resource not found for the segment 'users'
Solution:
- Verify the user ID or UPN is correct
- Ensure the user exists in your tenant
- Check that your app has permission to read users
Next Steps
Congratulations! You’ve successfully built your first Microsoft Graph application. Here’s what to explore next:Authentication Methods
Learn about interactive authentication, device code flow, and more
Core Concepts
Understand request builders, models, and collections in depth
Error Handling
Learn best practices for handling errors and retries
Working with Collections
Master paging, filtering, and working with large result sets
Additional Resources
- Microsoft Graph API Documentation
- Microsoft Graph Explorer - Test API calls in your browser
- Sample Applications
- Azure AD App Registration Best Practices
