Skip to content
Tracon

HttpTenantContext

Namespace Tracon · Assembly Tracon.AspNetCore.dll

Resolves the tenant from the current HTTP request.

public sealed class HttpTenantContext : ITenantContext
Inheritance
object ← HttpTenantContext
Implements
ITenantContext
Inherited Members
object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

The class is a singleton and works through Http.IHttpContextAccessor. A scoped implementation would produce a captive dependency in singleton services that inject ITenantContext (stores, decorators), and ASP.NET Core would report this as a startup error.

Resolution order:

  • If multi-tenancy is disabled → the default tenant.
  • If a claim type is set and the user passed authentication → the claim.
  • If header resolution is enabled → the header.
  • If none apply → the default tenant.

While the claim is set, the header is never read. Otherwise an authenticated user could access another tenant’s data simply by adding a header.

The current tenant’s identifier. Never returns empty.

public string TenantId { get; }

string

The value must be canonical — the form AmbientTenantScope.Normalize returns. The tenant identifier is matched case-insensitively across the product, and every layer achieves that by normalizing the value rather than by comparing case-insensitively. An implementation that returns Acme where another path resolves acme splits one tenant into two on PostgreSQL and SQLite, and merges them on SQL Server’s default collation — the authorization layer and the storage layer then disagree.

Says whether a tenant id is well-formed.

public static bool IsValidTenantId(string? tenantId)

tenantId string?

The value to check.

bool

true if the value is acceptable.

The id is a text column in the database and travels as a query parameter; SQL injection is not possible. A format constraint still applies: an unchecked value would be reflected as-is into logs, the audit trail, and the UI.