Azure AD
๐ Full Stack .NET Developer & React Enthusiast
๐จโ๐ป About Me: With 3+ years of experience, I'm passionate about crafting robust solutions and seamless user experiences through code.
๐ผ Expertise: Proficient in .NET Core API, ASP.NET MVC, React.js, and SQL. Skilled in backend architecture, RESTful APIs, and frontend development.
๐ Achievements: Led projects enhancing scalability by 50%, delivered ahead of schedule, and contributed to open-source initiatives.
๐ Future Focus: Eager to embrace new technologies and drive innovation in software development.
๐ซ Let's Connect: Open to new opportunities and collaborations. Reach me on LinkedIn or GitHub!
Azure AD Web API Authentication (Concepts)
๐ฏ Objective
Understand how Azure AD authentication works between a UI application and a backend Web API.
๐ง Core Architecture
A typical web system has two logical applications:
| Component | Role |
|---|---|
| Frontend (UI App) | React / Angular / Web UI |
| Backend (API App) | ASP.NET Core Web API |
Important concept:
Users never call APIs directly
Users interact with the UI
UI calls APIs on behalf of the user
๐ Authentication Flow
User opens the website.
Website redirects the user to Microsoft Entra ID (Azure AD) login.
User enters credentials.
Azure AD authenticates the user.
Azure AD returns a token to the UI app.
UI sends that token when calling the API.
API validates the token before responding.
๐ข Tenant Concept
A Tenant represents an organization.
Example:
Tenant
โโโ Users
โ โโโ Neil
โ โโโ Steve
โ
โโโ Applications
โโโ UI App
โโโ Backend API
Key points:
Users exist at tenant level
Applications also exist inside tenant
๐งพ App Registrations
Two app registrations are required.
| App | Purpose |
|---|---|
| UI App | Frontend application |
| Backend App | Web API |
๐ก Exposing APIs
Backend App โ Expose an API
This allows other apps to call it.
Example scope:
scope.any
โ Conditions for API Call
For a successful API call:
1๏ธโฃ User must be authenticated
2๏ธโฃ UI App must have permission to call API
Creating ASP.NET Core Web API with Azure AD
๐ฏ Objective
Create a Web API project with Azure AD authentication enabled.
๐ Creating the Project
In Microsoft Visual Studio:
Create New Project
Select ASP.NET Core Web API
Project name:
DemoApiAuthenticationUsingAzureAD
Uncheck HTTPS (for demo)
Authentication Type:
Microsoft Identity Platform
This automatically configures Azure AD support.
๐ Important Files
Controller
[Authorize]
public class WeatherForecastController : ControllerBase
{
}
Meaning:
- All endpoints require authentication.
Program.cs
builder.Services
.AddAuthentication()
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
This configures Azure AD authentication.
appsettings.json
"AzureAd": {
"TenantId": "",
"ClientId": "",
"Scopes": ""
}
These values come from Azure Portal.
โ ๏ธ First Run Result
API returns:
401 Unauthorized
Because Azure AD is not configured yet.
๐ง Temporary Fix
Disable authentication for testing:
[AllowAnonymous]
Now API works.
Testing in Postman
Endpoint example:
GET /weather/getWeatherData
Result:
200 OK
Implementing Azure AD Authentication
๐ฏ Objective
Configure Azure AD authentication for Web API.
Step 1 โ Backend App Registration
In Microsoft Azure Portal
Navigate:
Microsoft Entra ID
โ App Registrations
โ New Registration
Create:
My Backend App
Expose API
Open:
Expose an API
Add:
Application ID URI
Example:
api://backend-api-id
Create Scope
Example:
scope.any
Purpose:
Defines permission to access API.
Step 2 โ Frontend App Registration
Create another app:
My UI App
Platform:
Single Page Application
Add Redirect URI (Postman / frontend).
Add API Permissions
Navigate:
API Permissions
โ Add Permission
โ My APIs
Select:
My Backend App
Add permission:
scope.any
Enable Tokens
Under Authentication enable:
Access Tokens
ID Tokens
Step 3 โ Configure Web API
In appsettings.json
"AzureAd": {
"TenantId": "your-tenant-id",
"ClientId": "backend-app-client-id",
"Scopes": "scope.any"
}
Important:
Use Backend App ClientId, not UI app.
Service to Service Authentication (S2S)
๐ฏ Objective
Allow one service to call another service securely.
Example:
HRApp โ FinanceApp API
Backend API
FinanceApp
Example endpoint:
/financeapp/getbunetprofit
Controller:
[Authorize]
[Route("financeapp/getbunetprofit")]
public string GetProfit()
{
return "Net profit of BU is 1.4 million";
}
App Roles for S2S Authentication
Create App Role
In FinanceApp:
App roles
โ Create role
Example:
appro.read
Allowed member type:
Application
Meaning:
Only apps, not users.
HRApp Permissions
In HRApp:
API Permissions
โ Add Permission
โ FinanceApp
โ appro.read
Grant Admin Consent.
Protect API with Role
Controller:
[Authorize(Roles="appro.read")]
Only apps with this role can access.
Generating JWT Token (Client Credentials)
๐ฏ Objective
Generate JWT token for service-to-service API calls.
Required Fields
| Field | Description |
|---|---|
| Client ID | HRApp client ID |
| Client Secret | HRApp secret |
| Resource | FinanceApp API |
| Grant Type | client_credentials |
Flow
HRApp โ Azure AD โ JWT Token โ FinanceApp API
Steps:
HRApp requests token.
Azure AD validates credentials.
Azure AD issues JWT token.
HRApp sends token to FinanceApp API.
Calling API from Postman
Step 1 โ Verify API Requires Token
Request:
GET /financeapp/getbunetprofit
Response:
401 Unauthorized
Step 2 โ Configure OAuth2
In Postman
Authorization type:
OAuth 2.0
Grant Type:
Client Credentials
Required values:
| Field | Value |
|---|---|
| Token URL | Azure AD token endpoint |
| Client ID | HRApp |
| Client Secret | HRApp secret |
| Resource | FinanceApp API |
Step 3 โ Call API
Add token as:
Authorization: Bearer <token>
Response:
200 OK
Understanding Tenant and App Registration
What is a Tenant
A tenant represents an organization in Azure.
Example:
Tenant: ABC Technologies
Inside tenant:
Users
Applications
Permissions
Policies
Users in Tenant
Examples:
neil.david@abcd.com
mark.phillips@abcd.com
You can also add guest users.
App Registration Purpose
App registration allows applications to interact with Azure AD.
Example use cases:
Authentication
Access APIs
Read tenant users
Important Credentials
App registration generates:
Client ID
Client Secret
These are used by applications to authenticate with Azure AD.