Overview
The Social Media Activity Feed API uses JWT (JSON Web Token) Bearer Authentication to secure endpoints. This provides stateless, scalable authentication where the server doesn’t need to maintain session state.JWT Bearer Authentication
JWT authentication works by:- User provides credentials (username/password)
- Server validates credentials and generates a signed JWT
- Client includes the JWT in subsequent requests
- Server validates the token signature and extracts user claims
Token Validation Configuration
The API configures JWT validation inProgram.cs:19:
Jwt:Issuer: Identifies the token issuerJwt:Key: Secret key for signing tokens (usedotnet user-secretsfor local development)
Registration Flow
Endpoint: POST /api/register
Creates a new user account with hashed password. Request Body:- 201 Created: User successfully registered
- 400 Bad Request: Validation errors (invalid email/phone format)
- 409 Conflict: Username/email already exists (handled by database unique constraints)
Password Hashing with IPasswordHasher
The API uses ASP.NET Core Identity’sIPasswordHasher<string> (registered in Program.cs:18), which implements PBKDF2 with:
- Random salt per password
- Configurable iteration count
- Built-in versioning for algorithm upgrades
Login Flow
Endpoint: POST /api/login
Authenticates user and returns JWT access token. Request Body:- 200 OK: Login successful, returns user data and token
- 404 Not Found: Username not found
- 401 Unauthorized: Invalid password
Token Generation
TokenProvider Implementation (auth.tokenProvider.cs:12)
Token Claims
Each JWT contains:- sub (Subject): User ID (primary identifier)
- unique_name: Username
- email: User’s email address
- phone_number: User’s phone number
- exp (Expiration): Token expiration timestamp (60 minutes)
- iss (Issuer): Token issuer from configuration
- aud (Audience): Token audience (same as issuer)
Using Protected Endpoints
Including the Authorization Header
Once you have a token, include it in theAuthorization header with the Bearer scheme:
Marking Endpoints as Protected
Endpoints require authentication by calling.RequireAuthorization():
Authentication Flow
- Client sends request with
Authorization: Bearer <token>header - ASP.NET Core authentication middleware extracts and validates token
- If valid, middleware populates
HttpContext.Userwith claims - If invalid/missing, middleware returns 401 Unauthorized
- Endpoint handler receives authenticated user context
Complete Authentication Example
1. Register a New User
201 Created
2. Login to Get Token
3. Access Protected Endpoint
204 No Content (Follow successful)
Security Considerations
- Token Expiration: Tokens expire after 60 minutes - clients must re-authenticate
- Secret Key Storage: Use
dotnet user-secretslocally, environment variables in production - HTTPS Required: Always use HTTPS in production to prevent token interception
- Password Hashing: PBKDF2 with random salt prevents rainbow table attacks
- No Password in Responses: Password hashes are never returned in API responses