
ASP.NET Zero 15.2 Upgrade: Fix Backend Errors Before Updating Angular
Upgrade ASP.NET Zero 15.2 one layer at a time: stabilize the backend, check database migrations and Mapperly, test login and tenants, then merge Angular 21.
Read more →
Your AI feature works perfectly when a user clicks a button. Move the same work into a Hangfire background job, and suddenly the application says there is no logged-in user.
The request was authenticated. The AppService has its authorization rules. The job was created successfully. So where did the user go?
The problem is that a background job does not automatically inherit the user context from the HTTP request that started it.
This matters when an ASP.NET Zero application runs AI tasks, processes orders, generates reports, or performs other long-running operations outside the normal request-response cycle. Without the correct user and tenant context, a job may fail authorization checks or operate in the wrong tenant context.
The fix is to pass the required identity information into the job and restore the appropriate session context when the job executes. Last week we covered a backend-first ASP.NET Zero 15.2 upgrade. This week is the worker that loses the login after you queue the AI call. For the broader queue-and-notify pattern, see How to Run AI Actions in ASP.NET Zero with Background Jobs and Hangfire.
Consider a typical AI workflow.
A user submits a request through Angular. An AppService checks their permissions, saves the request, and queues a background job to call an AI model. The job later processes the result and updates the database.
The original HTTP request had access to the current user and tenant. The background worker runs separately, so it cannot assume that the original request context still exists.
Hangfire does not automatically preserve the current user ID, tenant ID, or HttpContext when it executes a background job. These values need to be passed or restored explicitly. See the Hangfire background methods documentation.
This can lead to errors such as:
Session.TenantId is nullThese are not necessarily AI model errors. They may be application-context problems that occur before the AI workflow can finish.
Start by identifying the context the job needs to complete its work correctly.
For a user-triggered AI job, that will often include:
Pass only the information the job actually needs. Avoid serializing an entire user object or relying on an HTTP request that will no longer exist when the worker executes.
A simple job-arguments class might look like this:
[Serializable]
public class AiJobArgs
{
public int? TenantId { get; set; }
public long? UserId { get; set; }
public long OrderId { get; set; }
public string JobId { get; set; }
}
Adjust the property types to match your application’s identifiers and job framework. The important part is that the required identity and business context are explicitly available to the worker.
In ASP.NET Zero applications based on ASP.NET Boilerplate, AbpSession.Use can temporarily override the current session values. This allows code running in a background job to establish the intended tenant and user context.
For example, the job can restore the session before calling application logic:
using (AbpSession.Use(args.TenantId, args.UserId))
{
await _orderAppService.ApproveFromAiAsync(
args.OrderId,
args.JobId
);
}
Check the method signature and supported behavior against the exact ASP.NET Zero and ABP version used by your application.
If the operation also requires an explicit Unit of Work tenant scope, establish that scope before accessing tenant-specific repositories. AbpSession.Use and CurrentUnitOfWork.SetTenantId address related but distinct aspects of context; use them according to your application’s Unit of Work and multi-tenancy configuration.
The essential rule is to establish the intended context before accessing repositories or invoking code that depends on the current session.
It may seem easier to remove [AbpAuthorize] from a method when a background job cannot pass its authorization check.
That can solve the immediate exception while creating a much bigger security problem.
If the method performs a sensitive operation, removing authorization without replacing it with an appropriate security boundary may allow the operation to run without the intended checks. We made the same point when reviewing permissions AI just added: a compile is not a grant.
Instead, decide what the job is supposed to represent:
Passing a user ID does not, by itself, prove that the user is still authorized to perform an operation. For sensitive actions, consider whether permissions or ownership should be revalidated when the job executes.
Also, do not use a host administrator identity simply to make an authorization exception disappear. That can turn a context bug into a cross-tenant security risk.
User context and tenant context are related, but they are not interchangeable.
For example, an AI job might be started by a tenant user who asks the system to analyze an order. If the worker runs without the intended tenant context, repository queries or tenant-aware operations may fail or return unexpected results.
A reliable workflow should:
Host-level jobs require special care because a null tenant ID can be valid for a host operation. Do not automatically interpret null as a missing tenant or switch to a privileged identity. Decide explicitly whether the operation belongs to the host or a tenant.
After the job runs, prove Tenant B still cannot see Tenant A’s result. That is the same second-tenant test from How to Catch a Tenant Data Leak in AI-Generated ASP.NET Zero Code.
Restoring context is only part of the workflow.
Long-running AI tasks usually outlive the original HTTP request. The frontend may need to show whether the job is queued, running, completed, or failed.
A practical design gives the operation a trackable job ID, stores its status, and provides a way for the intended user to retrieve the result. That could be polling, SignalR notifications, or another mechanism that fits your application.
Before returning a result, verify that the requesting user is allowed to access it and that the result belongs to the correct tenant. A job ID should not be treated as authorization on its own.
This is particularly important when AI output includes customer records, order information, documents, or other tenant-specific data.
When a Hangfire job fails with a missing-user or missing-tenant error, check the context in this order:
ASP.NET Zero support has documented this general pattern: pass the tenant and user IDs as background-job arguments and use AbpSession.Use to override the session values. See Running a Hangfire background job with the permissions of the user that started it.
Because the worker is not the original HTTP request. Capture TenantId and UserId when you enqueue, then restore them with AbpSession.Use before calling protected AppServices.
Often you also need the correct Unit of Work tenant scope. Confirm both session and UoW for the repositories you touch.
Not for sensitive operations. Restore identity or design an explicit system-level security boundary. Do not use a host admin identity as a shortcut.
No. Verify the requesting user and tenant before returning tenant-owned data.
That guide covers authorize → queue → notify. This one focuses on the failure mode where the worker runs without a user or tenant session.
Moving an AI call into Hangfire can make an application more responsive, but it also moves the work outside the original HTTP request.
The background worker cannot be expected to know who started the job or which tenant owns the data unless your application supplies that context.
Pass the required identifiers, restore the session before running tenant-aware logic, preserve the correct authorization boundaries, and make the result available only to the intended user.
The next time an ASP.NET Zero background job reports that no user is logged in, check the context being passed to the worker before rewriting the AI workflow.
How do you handle user and tenant context in your Hangfire jobs: explicit job arguments, session overrides, or a separate system-level workflow?
If your team is building AI workflows, background jobs, or multi-tenant SaaS features on ASP.NET Zero and needs help with Hangfire session restoration, authorization, and tenant isolation, our dedicated development team can help. Get in touch, or start with Development from Zero.
Sources: Hangfire, Background methods accessed 2026-10-23; ASP.NET Zero Support, Hangfire job permissions #10239 accessed 2026-10-23; ASP.NET Zero, Hangfire integration accessed 2026-10-23.
Get In Touch
Hire ASP.Net Zero Application Developers that will provide the perfect solution to your business issues. Our technical experts will provide you with a free consultation.

Upgrade ASP.NET Zero 15.2 one layer at a time: stabilize the backend, check database migrations and Mapperly, test login and tenants, then merge Angular 21.
Read more →
Planning an ASP.NET Zero upgrade? Inventory custom AppServices, Angular UI, and package deps first, then migrate in a controlled branch so your business features survive the version jump.
Read more →
AI-generated ASP.NET Zero code can compile and still leak Tenant A data to Tenant B. Review IMultiTenant, filter bypasses, host vs tenant logic, and run the second-tenant test before you merge.
Read more →