{"record":{"id":"aa0c6fce6c80c49f","repo":"fullstackhero/dotnet-starter-kit","slug":"user-not-found-userstatusservice","errorCode":null,"errorMessage":"User Not Found.","messagePattern":"User Not Found\\.","errorType":"exception","errorClass":"NotFoundException","httpStatus":404,"severity":"error","filePath":"src/Modules/Identity/Modules.Identity/Services/UserStatusService.cs","lineNumber":64,"sourceCode":"\n    private async Task<ToggleStatusContext> BuildToggleContextAsync(\n        string userId,\n        bool activateUser,\n        CancellationToken cancellationToken)\n    {\n        var actorId = currentUser.GetUserId();\n        if (actorId == Guid.Empty)\n        {\n            throw new UnauthorizedException(\"authenticated user required to toggle status\");\n        }\n\n        var actor = await userManager.FindByIdAsync(actorId.ToString())\n            ?? throw new UnauthorizedException(\"current user not found\");\n\n        var targetUser = await userManager.Users\n            .Where(u => u.Id == userId)\n            .FirstOrDefaultAsync(cancellationToken)\n            ?? throw new NotFoundException(\"User Not Found.\");\n\n        return new ToggleStatusContext(\n            ActorId: actorId,\n            Actor: actor,\n            TargetUser: targetUser,\n            ActivateUser: activateUser,\n            TenantId: multiTenantContextAccessor?.MultiTenantContext?.TenantInfo?.Id);\n    }\n\n    private async Task ValidateTogglePermissionsAsync(\n        ToggleStatusContext context,\n        CancellationToken cancellationToken)\n    {\n        if (!await userManager.IsInRoleAsync(context.Actor, RoleConstants.Admin))\n        {\n            await AuditPolicyFailureAsync(context, \"ActorNotAdmin\", cancellationToken);\n            throw new ForbiddenException(\"Only administrators can change user status.\");\n        }","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/fullstackhero/dotnet-starter-kit/blob/3f2959e683e9f83f13e55e1678c9119f63c7e8e5/src/Modules/Identity/Modules.Identity/Services/UserStatusService.cs#L46-L82","documentation":"UserStatusService.BuildToggleContextAsync throws NotFoundException(\"User Not Found.\") when the target user id from the toggle-status command does not match any ASP.NET Identity user row in the current tenant's database. It is the guard before any status change is applied, thrown via EF Core's FirstOrDefaultAsync over userManager.Users.","triggerScenarios":"Calling the toggle-user-status endpoint/command with a userId that does not exist in the database, that belongs to a different tenant (tenant filter excludes it), or that was deleted after the caller fetched its id.","commonSituations":"Stale client-side caches listing deleted users; running a query against the wrong tenant database in a multitenant deployment; copying a user id from another environment (dev/staging/prod); typos in manually built API requests.","solutions":["Verify the userId in the request actually exists: query AspNetUsers (filtered by the active tenant) for that id.","Confirm the request is sent with the correct tenant identifier/header so the tenant query filter includes the user.","Refresh the user list on the client so the id is not stale/deleted.","If the user should exist, run the DbMigrator with seed or restore the record."],"exampleFix":"// before\nawait mediator.Send(new ToggleUserStatusCommand { UserId = \"dead-id\" });\n// after\nvar user = await userManager.Users.FirstOrDefaultAsync(u => u.Id == id);\nif (user is not null)\n    await mediator.Send(new ToggleUserStatusCommand { UserId = user.Id });","handlingStrategy":"try-catch","validationCode":"var exists = await userManager.Users.AnyAsync(u => u.Id == userId, ct);\nif (!exists) throw new ValidationException($\"User {userId} does not exist.\");","typeGuard":null,"tryCatchPattern":"try { await mediator.Send(cmd); }\ncatch (NotFoundException) { /* show 'user no longer exists' UI state */ }","preventionTips":["Resolve user ids from a fresh server-side lookup rather than cached lists.","Always send the correct tenant header/id in multitenant calls.","Handle user deletion events by invalidating client caches.","Validate user existence in the command validator before dispatch."],"tags":["identity","user-management","not-found","multitenancy"],"backgroundTag":"user-not-found","analyzedSha":"3f2959e683e9f83f13e55e1678c9119f63c7e8e5","analyzedAt":"2026-09-15T22:20:53.684Z","contentChangedAt":"2026-09-15T22:20:53.684Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}