{"record":{"id":"044ec8a261a080c9","repo":"fullstackhero/dotnet-starter-kit","slug":"current-user-not-found","errorCode":null,"errorMessage":"current user not found","messagePattern":"current user not found","errorType":"exception","errorClass":"UnauthorizedException","httpStatus":401,"severity":"error","filePath":"src/Modules/Identity/Modules.Identity/Services/UserStatusService.cs","lineNumber":59,"sourceCode":"        if (string.IsNullOrWhiteSpace(multiTenantContextAccessor?.MultiTenantContext?.TenantInfo?.Id))\n        {\n            throw new UnauthorizedException(\"invalid tenant\");\n        }\n    }\n\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    {","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/fullstackhero/dotnet-starter-kit/blob/3f2959e683e9f83f13e55e1678c9119f63c7e8e5/src/Modules/Identity/Modules.Identity/Services/UserStatusService.cs#L41-L77","documentation":"Thrown by BuildToggleContextAsync when the authenticated actor's id (from claims) no longer resolves to an existing ApplicationUser via FindByIdAsync. The claims are valid but the backing user record is gone or invisible to the current tenant context.","triggerScenarios":"Toggling another user's status with a token issued for a user that was deleted; actor's user row filtered out by tenant query filters; database was reseeded while old tokens remained valid.","commonSituations":"Stale long-lived JWTs after a database reset or user hard-delete; cross-tenant token reuse; migration/seed scripts removing users without revoking sessions.","solutions":["Re-authenticate to get a token bound to a live user.","Verify the actor's user row exists in the current tenant database.","If users were reseeded, invalidate outstanding tokens (reset security stamp / revoke sessions).","Check tenant filters — the actor may exist in another tenant."],"exampleFix":"// before: keeps retrying with a stale token\nawait toggleStatus(targetId, ct); // UnauthorizedException: current user not found\n// after: refresh credentials first\nawait auth.relogin();\nawait toggleStatus(targetId, ct);","handlingStrategy":"retry","validationCode":"const me = await apiFetch(\"/api/v1/users/current\");\nif (!me || !me.id) throw new Error(\"Token is bound to a missing user — re-authenticate\");","typeGuard":null,"tryCatchPattern":"try { await toggleStatus(userId); } catch (e) {\n  if (e.status === 401 && /current user not found/i.test(e.message)) { await logoutAndRelogin(); return retryOnce(); }\n  throw e;\n}","preventionTips":["Rotate tokens after reseeding or hard-deleting users (reset security stamps).","Keep tokens short-lived so stale identities expire quickly.","Verify the actor exists in the tenant database before administrative operations."],"tags":["authentication","identity","stale-token"],"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-23T08:17:48.524Z"}