IAM Service .NET 10
Identity and Access Management Service built with .NET 10, ASP.NET Core Identity, and Duende IdentityServer following DDD, CQRS, and Clean Architecture patterns.
Overview
This service provides OAuth2/OpenID Connect authentication and authorization:
- OAuth2/OIDC Server - Duende IdentityServer for token management
- User Management - Registration, profile, soft-delete
- Role-Based Access Control - User roles and permissions
- Token Management - Access (15 min), Refresh (7 days) tokens
- Email Verification - SMTP-based email confirmation
- Two-Factor Authentication (2FA) - TOTP with QR code setup
- Social Login - Google and Facebook OAuth integration
- CQRS Pattern - MediatR for Commands/Queries
- Clean Architecture - Domain, Infrastructure, API layers
Tech Stack
| Technology |
Purpose |
| .NET 10 |
Runtime |
| ASP.NET Core Identity |
User/Role management |
| Duende IdentityServer |
OAuth2/OIDC server |
| EF Core + PostgreSQL |
Data persistence |
| Redis |
Distributed caching |
| MediatR |
CQRS pattern |
| FluentValidation |
Request validation |
| Serilog |
Structured logging |
Quick Start
1. Prerequisites
- .NET SDK 10.0.101+
- Docker (for PostgreSQL)
2. Configure Environment
3. Run with Docker
Service available at: http://localhost:5001
4. Run Locally
Database Migrations
Prerequisites
Create Migration
Apply Migration
Neon Database Setup
- Create database on Neon Console
- Update
appsettings.Development.json with connection string
- Run:
dotnet ef database update ...
API Endpoints
Authorization Policies
Note: All API endpoints require authentication (Bearer JWT Token).
Some endpoints require specific roles as shown below.
| Policy |
Required Role |
Applied To |
RequireSuperAdmin |
SuperAdmin |
PAM (Privileged Access Management) |
RequireAdmin |
Admin, SuperAdmin |
User/Role/Group/Organization management |
RequireAuditor |
Auditor, Admin, SuperAdmin |
Audit logs, Compliance reports |
OwnerOrAdmin |
Owner or Admin |
User self-service profile management |
Authorization by Controller:
| Controller |
Policy |
Description |
| Users (GET /users, DELETE) |
RequireAdmin |
List users, delete user |
| Users (GET/PUT /{id}) |
OwnerOrAdmin |
User access own profile or Admin access any |
| Roles |
RequireAdmin |
Role management |
| Organizations |
RequireAdmin |
Organization management |
| Groups |
RequireAdmin |
Group management |
| Access Requests |
RequireAdmin |
Access request workflow |
| Access Reviews |
RequireAdmin |
Periodic access review |
| Privileged Access |
RequireSuperAdmin |
PAM - most sensitive |
| Audit |
RequireAuditor |
View audit logs |
| Compliance |
RequireAuditor |
Compliance reports |
| Verifications |
RequireAdmin |
Identity verification |
Authentication (/api/v1/auth)
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/auth/register |
Register new user |
❌ |
POST |
/connect/token |
OAuth2 token endpoint |
❌ |
POST |
/api/v1/auth/change-password |
Change password |
✅ |
POST |
/api/v1/auth/logout |
Logout (revoke tokens) |
✅ |
Email Verification (/api/v1/auth)
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/auth/send-verification-email |
Send email verification link |
✅ |
POST |
/api/v1/auth/confirm-email |
Confirm email with token |
❌ |
Two-Factor Authentication (/api/v1/auth/2fa)
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/auth/2fa/enable |
Enable 2FA (get QR code) |
✅ |
POST |
/api/v1/auth/2fa/verify |
Verify TOTP code & activate |
✅ |
POST |
/api/v1/auth/2fa/disable |
Disable 2FA |
✅ |
Social Login (/api/v1/auth)
| Method |
Endpoint |
Description |
Auth |
GET |
/api/v1/auth/external-login/{provider} |
Initiate OAuth flow (Google/Facebook) |
❌ |
GET |
/api/v1/auth/external-callback |
Handle OAuth callback |
❌ |
GET |
/api/v1/auth/linked-accounts |
Get linked OAuth providers |
✅ |
User Management (/api/v1/users)
| Method |
Endpoint |
Description |
Auth |
GET |
/api/v1/users |
List users (paginated) |
✅ |
GET |
/api/v1/users/me |
Get current user |
✅ |
GET |
/api/v1/users/{id} |
Get user by ID |
✅ |
PUT |
/api/v1/users/{id} |
Update user |
✅ |
DELETE |
/api/v1/users/{id} |
Delete user (soft) |
✅ |
GET |
/api/v1/users/{id}/roles |
Get user's assigned roles |
✅ |
GET |
/api/v1/users/{id}/permissions |
Get user's effective permissions |
✅ |
User Profiles (/api/v1/users/{userId}/profile)
| Method |
Endpoint |
Description |
Auth |
GET |
/api/v1/users/{userId}/profile |
Get user profile |
✅ |
PUT |
/api/v1/users/{userId}/profile |
Update user profile |
✅ |
PUT |
/api/v1/users/{userId}/profile/attributes/{key} |
Set profile attribute |
✅ |
Roles Management (/api/v1/roles)
| Method |
Endpoint |
Description |
Auth |
GET |
/api/v1/roles |
List roles (paginated) |
✅ |
GET |
/api/v1/roles/{id} |
Get role by ID |
✅ |
POST |
/api/v1/roles |
Create new role |
✅ |
PUT |
/api/v1/roles/{id} |
Update role |
✅ |
DELETE |
/api/v1/roles/{id} |
Delete role |
✅ |
POST |
/api/v1/roles/users/{userId} |
Assign role to user |
✅ |
DELETE |
/api/v1/roles/users/{userId}/{roleName} |
Remove role from user |
✅ |
Identity Verifications (/api/v1/verifications)
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/verifications/phone |
Request phone verification |
✅ |
POST |
/api/v1/verifications/email |
Request email verification |
✅ |
POST |
/api/v1/verifications/{id}/confirm |
Confirm verification with code |
✅ |
Health Endpoints
| Endpoint |
Purpose |
/health |
Full health status |
/health/live |
Liveness probe |
/health/ready |
Readiness probe |
Organizations (/api/v1/organizations) - Phase 2
| Method |
Endpoint |
Description |
Auth |
GET |
/api/v1/organizations/{id} |
Get organization by ID |
✅ |
GET |
/api/v1/organizations/slug/{slug} |
Get organization by slug |
✅ |
POST |
/api/v1/organizations |
Create organization |
✅ |
PUT |
/api/v1/organizations/{id} |
Update organization |
✅ |
DELETE |
/api/v1/organizations/{id} |
Archive organization |
✅ |
GET |
/api/v1/organizations/{id}/hierarchy |
Get hierarchy |
✅ |
GET |
/api/v1/organizations/{id}/children |
Get child orgs |
✅ |
Groups (/api/v1/groups) - Phase 2
| Method |
Endpoint |
Description |
Auth |
GET |
/api/v1/groups |
List groups |
✅ |
GET |
/api/v1/groups/{id} |
Get group by ID |
✅ |
POST |
/api/v1/groups |
Create group |
✅ |
DELETE |
/api/v1/groups/{id} |
Delete group |
✅ |
POST |
/api/v1/groups/{id}/members |
Add member |
✅ |
DELETE |
/api/v1/groups/{id}/members/{userId} |
Remove member |
✅ |
Access Requests (/api/v1/access-requests) - Phase 3A
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/access-requests |
Create access request |
✅ |
GET |
/api/v1/access-requests |
List requests |
✅ |
GET |
/api/v1/access-requests/{id} |
Get request by ID |
✅ |
POST |
/api/v1/access-requests/{id}/submit |
Submit request |
✅ |
POST |
/api/v1/access-requests/{id}/approve |
Approve |
✅ |
POST |
/api/v1/access-requests/{id}/reject |
Reject |
✅ |
DELETE |
/api/v1/access-requests/{id} |
Cancel request |
✅ |
GET |
/api/v1/access-requests/pending |
Pending requests |
✅ |
Access Reviews (/api/v1/access-reviews) - Phase 3B
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/access-reviews |
Create access review |
✅ |
GET |
/api/v1/access-reviews/{id} |
Get review by ID |
✅ |
POST |
/api/v1/access-reviews/{id}/items |
Add item |
✅ |
POST |
/api/v1/access-reviews/{id}/start |
Start review |
✅ |
POST |
/api/v1/access-reviews/{id}/items/{itemId}/review |
Certify/Revoke |
✅ |
POST |
/api/v1/access-reviews/{id}/complete |
Complete |
✅ |
Privileged Access (/api/v1/privileged-access) - Phase 3B PAM
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/privileged-access/request |
Request JIT access |
✅ |
GET |
/api/v1/privileged-access/active |
Active grants |
✅ |
POST |
/api/v1/privileged-access/{id}/revoke |
Revoke access |
✅ |
Audit (/api/v1/audit) - Phase 4A
| Method |
Endpoint |
Description |
Auth |
GET |
/api/v1/audit/logs |
Get audit logs (filtered) |
✅ |
Compliance (/api/v1/compliance) - Phase 4A
| Method |
Endpoint |
Description |
Auth |
POST |
/api/v1/compliance/reports |
Generate report |
✅ |
GET |
/api/v1/compliance/reports |
List reports |
✅ |
GET |
/api/v1/compliance/reports/{id} |
Report detail |
✅ |
POST |
/api/v1/compliance/reports/{id}/complete |
Complete report |
✅ |
GET |
/api/v1/compliance/violations |
Unresolved violations |
✅ |
Authentication Flow
Step 1: Register a New User
Response:
Step 2: Login (Password Grant)
Response:
Step 3: Use Access Token
Step 4: Refresh Token
Step 5: Logout
Client Credentials (Service-to-Service)
For service-to-service authentication without user context:
Supported OAuth2 Grant Types
| Grant Type |
Use Case |
Requires User |
password |
User login from trusted apps |
Yes |
refresh_token |
Token renewal |
No (uses refresh token) |
client_credentials |
Service-to-service |
No |
Email Verification
Send Verification Email
Response:
Confirm Email
Two-Factor Authentication (2FA)
Enable 2FA
Response:
Verify 2FA Code
Disable 2FA
Social Login
Initiate OAuth Flow
Redirect user to:
Get Linked Accounts
Response:
Configuration
Environment Variables
| Variable |
Description |
Required |
ASPNETCORE_ENVIRONMENT |
Environment |
No (default: Development) |
DATABASE_URL |
PostgreSQL connection |
Yes |
JWT_SECRET |
JWT signing secret (32+ chars) |
Yes |
REDIS_HOST |
Redis server host |
No (default: localhost) |
REDIS_PORT |
Redis server port |
No (default: 6379) |
REDIS_PASSWORD |
Redis password |
No |
REDIS_DATABASE |
Redis database number |
No (default: 0) |
Token Lifetimes
| Token |
Lifetime |
| Access Token |
15 minutes |
| Refresh Token |
7 days |
Redis Caching
The service uses Redis for distributed caching with the ICacheService interface.
Configuration
Add Redis settings in appsettings.json:
Or use environment variables:
ICacheService Interface
Usage Examples
Basic Get/Set:
Get or Set Pattern (Cache-Aside):
Token Blacklisting (for Logout):
iam-service-net/
├── src/
│ ├── IamService.API/ # Controllers, CQRS
│ │ ├── Controllers/ # AuthController, UsersController
│ │ └── Application/ # Commands, Queries, Validations
│ ├── IamService.Domain/ # Domain entities
│ │ ├── AggregatesModel/ # UserAggregate, RoleAggregate
│ │ ├── Events/ # Domain events
│ │ └── Exceptions/ # Domain exceptions
│ └── IamService.Infrastructure/ # Data access
│ ├── IamServiceContext.cs # DbContext with Identity
│ └── Repositories/ # Repository implementations
├── tests/
│ ├── IamService.UnitTests/
│ └── IamService.FunctionalTests/
├── docs/
│ ├── en/ # English documentation
│ └── vi/ # Vietnamese documentation
├── Dockerfile
└── docker-compose.yml
Docker
Resources
License
Proprietary - GoodGo Platform