Microsoft OAuth Setup
This guide walks you through setting up Microsoft OAuth authentication (Microsoft Entra ID, formerly Azure AD) for your UIGen application.
Prerequisites
- A Microsoft account (personal or work/school account)
- Your UIGen application's redirect URI (e.g.,
http://localhost:3000/auth/callbackfor development)
Step 1: Access Azure Portal
- Go to the Azure Portal
- Sign in with your Microsoft account
- If you don't have an Azure subscription, you can still register apps for free
Step 2: Register a New Application
- In the Azure Portal, search for Microsoft Entra ID (or Azure Active Directory)
- Click on Microsoft Entra ID in the search results
- In the left sidebar, click App registrations
- Click New registration
Step 3: Configure Application Registration
Fill in the registration details:
Name:
- Enter your application name (e.g., "My UIGen App")
- This name will be shown to users during consent
Supported account types:
- Accounts in any organizational directory and personal Microsoft accounts (recommended for most apps)
- Allows both work/school and personal Microsoft accounts
- Or choose a more restrictive option based on your needs
Redirect URI:
- Select platform: Web
- Enter your redirect URI:
- Development:
http://localhost:3000/auth/callback - Production:
https://yourdomain.com/auth/callback
- Development:
Click Register
Step 4: Get Your Application Credentials
After registration, you'll see the app overview:
- Application (client) ID: Copy this value - you'll need it for UIGen configuration
- Directory (tenant) ID: Note this value (needed for some configurations)
Step 5: Add Additional Redirect URIs (Optional)
If you need multiple redirect URIs:
- In your app, go to Authentication in the left sidebar
- Under Platform configurations > Web, click Add URI
- Add additional URIs:
- Staging:
https://staging.yourdomain.com/auth/callback - Production:
https://yourdomain.com/auth/callback
- Staging:
- Click Save
Step 6: Configure Token Settings
- Still in Authentication, scroll to Implicit grant and hybrid flows
- Enable:
- ✅ ID tokens (for OpenID Connect)
- Click Save
Step 7: Configure API Permissions
- In the left sidebar, click API permissions
- You should see Microsoft Graph with User.Read permission by default
- To add more permissions, click Add a permission:
- Select Microsoft Graph
- Select Delegated permissions
- Add permissions as needed (e.g.,
email,profile,openid)
- Click Add permissions
Default permissions are sufficient for basic authentication.
Step 8: Configure UIGen
Add the Microsoft OAuth provider to your OpenAPI spec:
openapi: 3.0.0
info:
title: My API
version: 1.0.0
x-uigen-auth:
providers:
- provider: microsoft
clientId: ${MICROSOFT_CLIENT_ID}
redirectUri: ${MICROSOFT_REDIRECT_URI}
scopes:
- openid
- email
- profile
Step 9: Set Environment Variables
Create a .env file or set environment variables:
export MICROSOFT_CLIENT_ID="12345678-1234-1234-1234-123456789012"
export MICROSOFT_REDIRECT_URI="http://localhost:3000/auth/callback"
Step 10: Test Your Integration
-
Start your UIGen application:
npx @uigen-dev/cli serve openapi.yaml -
Navigate to
http://localhost:3000 -
Click the Continue with Microsoft button
-
You should be redirected to Microsoft's login page
-
Sign in with your Microsoft account
-
Grant the requested permissions
-
You should be redirected back to your application and logged in
Scopes Explained
Default Scopes
UIGen requests these scopes by default:
| Scope | Description | Data Accessed |
|---|---|---|
openid |
OpenID Connect authentication | User's unique Microsoft ID |
email |
Email address | User's primary email address |
profile |
Basic profile information | Name, profile picture |
Additional Scopes
You can request additional Microsoft Graph scopes if needed:
| Scope | Description | Use Case |
|---|---|---|
User.Read |
Read user profile | Full profile access (default) |
User.ReadBasic.All |
Read all users' basic profiles | User directory access |
Calendars.Read |
Read user calendars | Calendar integration |
Mail.Read |
Read user mail | Email integration |
Files.Read |
Read user files | OneDrive integration |
offline_access |
Refresh token access | Long-lived sessions |
See Microsoft Graph Permissions Reference for a complete list.
Account Types Explained
Personal Microsoft Accounts
- Outlook.com, Hotmail.com, Live.com accounts
- Xbox, Skype accounts
- Consumer Microsoft services
Work or School Accounts
- Microsoft 365 accounts
- Azure AD accounts
- Organizational accounts
Configuration Options
When registering your app, you chose one of:
-
Accounts in this organizational directory only (Single tenant)
- Only users from your organization
- Most restrictive
-
Accounts in any organizational directory (Multi-tenant)
- Users from any Microsoft 365 organization
- No personal accounts
-
Accounts in any organizational directory and personal Microsoft accounts (Multi-tenant + personal)
- Both work/school and personal accounts
- Most flexible (recommended)
-
Personal Microsoft accounts only
- Only consumer accounts
- No organizational accounts
Production Checklist
Before deploying to production:
- Update redirect URI to your production domain
- Add production domain to redirect URIs in Azure
- Store Application ID securely (environment variables, secrets manager)
- Never commit client secrets to version control (if using confidential client flow)
- Enable HTTPS for your production domain
- Test the complete OAuth flow in production
- Configure branding (logo, colors) in Azure portal
- Add Privacy Policy URL in app branding
- Add Terms of Service URL in app branding
- Review and minimize requested permissions
Troubleshooting
"AADSTS50011: The redirect URI specified in the request does not match"
Problem: The redirect URI doesn't match what's configured in Azure.
Solution:
- Check that the redirect URI in your OpenAPI spec exactly matches the one in Azure app registration
- Ensure there are no trailing slashes
- Verify protocol (http vs https) matches
- Azure requires exact match including protocol, domain, and path
"AADSTS65001: The user or administrator has not consented to use the application"
Problem: User hasn't granted consent for the requested permissions.
Solution:
- This is normal for first-time users
- User will see consent screen and can grant permissions
- For organizational accounts, admin may need to grant consent
"AADSTS700016: Application not found in the directory"
Problem: Application ID is incorrect or app was deleted.
Solution:
- Verify your
MICROSOFT_CLIENT_IDenvironment variable is correct - Check that the app still exists in Azure portal
- Ensure you're using the Application (client) ID, not the Object ID
"AADSTS50105: The signed in user is not assigned to a role for the application"
Problem: User is not assigned to the application (for apps requiring assignment).
Solution:
- Go to Enterprise applications in Azure portal
- Find your app and go to Users and groups
- Add the user or disable User assignment required in Properties
Email Not Returned
Problem: User's email is not included in the token.
Solution:
- Ensure you're requesting the
emailscope - For work/school accounts, email is usually available
- For personal accounts, user must have verified email
- Check that
emailclaim is included in token configuration
"AADSTS90094: The grant requires admin permission"
Problem: Requested permission requires admin consent.
Solution:
- Some permissions require admin consent in organizational tenants
- Contact your organization's admin to grant consent
- Or request only permissions that don't require admin consent
Admin Consent
When Admin Consent Is Required
For organizational accounts, some permissions require admin consent:
- Application permissions (app-only access)
- High-privilege delegated permissions
- Organization-wide data access
How to Request Admin Consent
Option 1: Admin Consent URL
Construct an admin consent URL:
https://login.microsoftonline.com/{tenant}/adminconsent
?client_id={client_id}
&redirect_uri={redirect_uri}
Option 2: Azure Portal
Admin can grant consent in Azure portal:
- Go to Enterprise applications
- Find your app
- Go to Permissions
- Click Grant admin consent for [Organization]
Branding Your Application
Customize how your app appears to users:
- In your app registration, go to Branding & properties
- Configure:
- Name: Display name shown to users
- Logo: 240x240 PNG image
- Home page URL: Your application's homepage
- Terms of service URL: Link to your terms
- Privacy statement URL: Link to your privacy policy
- Click Save
Multi-Tenant Applications
If your app supports multiple organizations:
Configuration
- Set Supported account types to multi-tenant during registration
- Use common endpoint in authorization URL:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize
Considerations
- Each tenant admin must consent to your app
- Users from any organization can sign in
- Implement tenant isolation in your application
- Store tenant ID with user data
Security Best Practices
- Never expose client secrets: Keep secrets server-side only (if using confidential client flow)
- Use HTTPS in production: Always use HTTPS for production redirect URIs
- Validate state parameter: UIGen automatically validates the state parameter for CSRF protection
- Request minimum scopes: Only request the scopes your application actually needs
- Implement token validation: Validate ID tokens and access tokens
- Use token caching: Cache tokens to reduce authentication requests
- Monitor sign-ins: Review sign-in logs in Azure portal
Token Lifetime
Microsoft tokens have specific lifetimes:
| Token Type | Default Lifetime | Renewable |
|---|---|---|
| Access Token | 1 hour | No (use refresh token) |
| Refresh Token | 90 days (inactive) | Yes (rolling) |
| ID Token | 1 hour | No |
UIGen automatically handles token refresh using refresh tokens.
Conditional Access
Organizations can enforce conditional access policies:
- Multi-factor authentication (MFA)
- Device compliance
- Location restrictions
- Risk-based access
Your application should handle:
- MFA challenges
- Conditional access errors
- Graceful fallback for blocked users
Next Steps
- OAuth Security Best Practices - Learn about OAuth security
- OAuth Troubleshooting - Common issues and solutions
- Google OAuth Setup - Set up Google OAuth
- GitHub OAuth Setup - Set up GitHub OAuth