The TaskHub.Shared.Authorization.Identity module provides a comprehensive, production-ready identity and access management (IAM) implementation. It bridges standard JWT-based authentication with TaskHub’s specific business requirements, such as real-time user status tracking and granular claim resolution.
Unlike standard JWT middleware that only validates the signature, the UserStatusMiddleware performs real-time checks on the user’s current status:
Authorization header.ITokenService to parse and validate the token, populating the ClaimsPrincipal.401 Unauthorized or 403 Forbidden with a structured Result body, preventing the request from reaching the application layer.The UserService is registered with a Scoped lifetime, ensuring it is fresh for every HTTP request:
UserId).HttpContext.User in every service.IUserService| Property / Method | Type | Description |
| :— | :— | :— |
| UserId | Guid | The unique identifier of the currently authenticated user. |
| Roles | IEnumerable<string> | List of roles assigned to the user. |
| IsInRole(string role) | bool | Helper to check role membership. |
| GetClaim(string type) | string? | Retrieves a specific claim value by its type. |
ITokenService| Method | Description |
| :— | :— |
| string CreateToken(User user) | Generates a new JWT token for the given user. |
| ClaimsPrincipal GetPrincipal(string token) | Validates a token and returns its principal. |
[Authorize(Roles = "Admin, Manager")]
[HttpPost("secure-action")]
public async Task<Result> ExecuteSecureAction()
{
var userId = _userService.UserId;
// ...
}
public async Task<ValueResult<string>> LoginAsync(LoginRequest req)
{
var user = await _repo.GetByEmailAsync(req.Email);
if (!PasswordHasher.Verify(user.Password, req.Password))
return ResultFactory.OnFailed(401, "Invalid credentials");
var token = _tokenService.CreateToken(user);
return ResultFactory.OnSuccess(token);
}
appsettings.json)"Jwt": {
"Issuer": "TaskHub.Identity",
"Audience": "TaskHub.Services",
"Key": "at-least-32-character-long-secret-key",
"ExpiryInMinutes": 1440,
"ValidateLifetime": true,
"ValidateIssuer": true,
"ValidateAudience": true
}
Every authenticated request is tagged with:
user.id: The ID of the authenticated user.user.roles: Comma-separated list of roles.auth_success_total: Counter for successful authentications.auth_failure_total: Counter for failed attempts (tagged by reason: invalid_token, blocked_user).IUserService: Always inject IUserService instead of accessing HttpContext.User directly to ensure consistent behavior.UserStatusMiddleware in production, as it is the only way to immediately revoke access for blocked users.