wasAsked.

Authorization Handling: How does your backend application handle authorization? How do you ensure User A can't see User B's trips?

Dodge Construction Network · Senior Developer · technical

Published

Asked in 2026 · Authentication & Security

Community answers

Posted by wasAsked editorial collection

The key principle: authorization is enforced on the server for every request, using the identity from a validated token — never trusting IDs or roles sent by the client. 1. Authenticate the user The API validates the JWT or session cookie (signature, issuer, audience, expiry). The user ID comes from a claim in the token (for example "sub"), not from a request parameter. 2. Role and policy checks (what kind of actions) [Authorize(Policy = "TravellerOnly")] restricts endpoints by role or claim. That is necessary but not enough — every traveller can call GET /trips/{id}. 3. Resource-level checks (which specific records) This prevents User A from seeing User B's trips, an attack known as IDOR (Insecure Direct Object Reference). Option A — filter queries by owner: [HttpGet("{id}")] public async Task<IActionResult> GetTrip(Guid id, CancellationToken ct) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var trip = await db.Trips.AsNoTracking() .Where(t => t.Id == id && t.OwnerId == userId) .Select(t => new TripDto(t.Id, t.Destination, t.StartDate)) .FirstOrDefaultAsync(ct); return trip is null ? NotFound() : Ok(trip); // 404 avoids revealing that the trip exists } Lists use the same filter: WHERE OwnerId = @currentUser. Option B — resource-based authorization with IAuthorizationService and a handler such as TripOwnerHandler, useful when rules are complex (owner, shared with friends, travel agent, admin). 4. Make it systematic - Centralise the rule: EF Core global query filters, a repository that always applies the owner filter, or database row-level security. - Use non-guessable IDs (GUIDs) as defence in depth — but never as the only protection. - Apply the same checks to updates, deletes, file downloads, exports and search endpoints. 5. Test it Automated integration tests that sign in as User A and try to read, list, update and delete User B's trips, expecting 404 or 403. Log and monitor access-denied events.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics