# HttpTenantContext

> Tracon documentation. Tracon is a .NET package family that adds a control
> plane on Microsoft Agent Framework, published as NuGet packages that run
> inside your own ASP.NET Core application.
>
> Page: https://tracon.dev/api/tracon.httptenantcontext/
> Summary: HttpTenantContext: Resolves the tenant from the current HTTP request.

<div class="api-provenance">Namespace Tracon · Assembly <code>Tracon.AspNetCore.dll</code></div>

Resolves the tenant from the current HTTP request.

```csharp
public sealed class HttpTenantContext : ITenantContext
```

<dl class="api-relations"><dt>Inheritance</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object">object</a> ← <a href="/api/tracon.httptenantcontext/">HttpTenantContext</a></dd><dt>Implements</dt><dd><a href="/api/tracon.itenantcontext/">ITenantContext</a></dd><dt>Inherited Members</dt><dd><a href="https://learn.microsoft.com/dotnet/api/system.object.gettype">object.GetType()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.tostring">object.ToString()</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object)">object.Equals(object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)">object.Equals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.referenceequals">object.ReferenceEquals(object?, object?)</a>, <a href="https://learn.microsoft.com/dotnet/api/system.object.gethashcode">object.GetHashCode()</a></dd></dl>

## Remarks

The class is a **singleton** and works through
`Http.IHttpContextAccessor`. A scoped implementation would produce a
captive dependency in singleton services that inject [ITenantContext](/api/tracon.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.

## Properties

### <a id="Tracon_HttpTenantContext_TenantId"></a> TenantId

The current tenant's identifier. Never returns empty.

```csharp
public string TenantId { get; }
```

#### Property Value

 [string](https://learn.microsoft.com/dotnet/api/system.string)

#### Remarks

**The value must be canonical** — the form
[AmbientTenantScope.Normalize](/api/tracon.ambienttenantscope/#Tracon_AmbientTenantScope_Normalize_System_String_) 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.

## Methods

### <a id="Tracon_HttpTenantContext_IsValidTenantId_System_String_"></a> IsValidTenantId\(string?\)

Says whether a tenant id is well-formed.

```csharp
public static bool IsValidTenantId(string? tenantId)
```

#### Parameters

`tenantId` [string](https://learn.microsoft.com/dotnet/api/system.string)?

The value to check.

#### Returns

 [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

[true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) if the value is acceptable.

#### Remarks

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.
