ASP.NET Core Integration
MultiTenant provides first-class support for ASP.NET Core through a dedicated package that adds middleware,
HttpContext helpers, and several ASP.NET Core-specific strategies.
Package Installation
For most ASP.NET Core apps install the Finbuckle.MultiTenant.AspNetCore package:
dotnet add package Finbuckle.MultiTenant.AspNetCore
This package depends on Finbuckle.MultiTenant and transitively brings in everything needed for
ASP.NET Core integration, including ASP.NET Core-specific strategies.
How the Middleware Works
When UseMultiTenant<TId>() is added to the pipeline, the middleware automatically handles tenant resolution
for each HTTP request:
- When the host creates the
HttpContextfor a request, a fresh ambient tenant scope begins (see The Per-Request Ambient Tenant Scope). The scope is in effect for the whole pipeline, including middleware that runs beforeUseMultiTenant<TId>(), and ends when theHttpContextis disposed. - The middleware calls
ITenantResolver.ResolveAsync(HttpContext). - The resolver iterates through registered strategies (which receive the
HttpContext) to find a tenant identifier, then queries stores to find a matchingTenantInfo. - If a tenant is found, the middleware sets
TenantInfoon theITenantContext<TTenantInfo, TId>for the request's scope. If earlier middleware already set a tenant, resolution is skipped and that tenant is kept. - All services resolved during the request — including
IOptions<T>,IOptionsSnapshot<T>,IOptionsMonitor<T>, andIMultiTenantDbContext<TId>— automatically see the resolved tenant.
This means you never need to manually call ITenantResolver<TId> or populate ITenantContext<TId> in an ASP.NET Core
app — the middleware wires everything together.
Configuring the Middleware
Service Registration
Register MultiTenant services in Program.cs using AddMultiTenant<TTenantInfo, TId> along with at least one
strategy and one store:
using Finbuckle.MultiTenant;
var builder = WebApplication.CreateBuilder(args);
// ...add other app services
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithHostStrategy()
.WithConfigurationStore();
var app = builder.Build();
See Configuration and Usage for more details on AddMultiTenant and its builder methods.
Adding the Middleware
Call UseMultiTenant<TId>() to add the middleware to the request pipeline.
// add the MultiTenant middleware
app.UseMultiTenant<string>();
// ...add other middleware
app.Run();
Middleware ordering is important. Place
UseMultiTenant<TId>()before any middleware that requires per-tenant behavior, such asUseAuthentication(),UseAuthorization(), and any other components that read per-tenant options or services.
If UseRouting() is explicitly called in the pipeline it must come before UseMultiTenant<TId>() when
using the Route Strategy.
The Per-Request Ambient Tenant Scope
AddHttpAmbientTenantScope() registers a startup filter, ahead of any other startup filters, that adds a small
middleware at the very start of the request pipeline. That middleware begins a new ambient tenant scope for each
request, so every request starts with its own unresolved tenant scope that is in effect for all later middleware
(including middleware added before UseMultiTenant<TId>() or by other startup filters), endpoints, and services. The
scope ends automatically when the request pipeline completes.
Every ASP.NET Core builder method (all the With...Strategy() methods in this package,
WithPerTenantAuthentication(), BypassWhen(), ShortCircuitWhen(), and their variants) calls
AddHttpAmbientTenantScope() for you. Call it yourself only if you configure MultiTenant for ASP.NET Core with
general-purpose methods alone, such as WithStaticStrategy() or WithDelegateStrategy():
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithDelegateStrategy(ctx => Task.FromResult<string?>("initech"))
.WithConfigurationStore()
.AddHttpAmbientTenantScope();
Calling it more than once has no further effect. If it was never called, UseMultiTenant<TId>() throws a
MultiTenantException at startup. It also throws at startup if ITenantScopeProvider was replaced with a scoped or
transient registration, because the scope begun for each request must be the same one the middleware and
ITenantContext use. If the middleware runs for a request with no active scope (for example, because
the pipeline was built without the host's startup filters), it throws a MultiTenantException for that request.
Long-Lived Connections
Some requests stay open for a long time, such as WebSockets (including SignalR over WebSockets), server-sent events, and streaming gRPC calls. For these, the request's tenant scope lasts as long as the connection:
- The tenant is resolved once, when the connection opens. It applies to everything that runs as part of that request for the life of the connection, such as each SignalR hub method call or each message on a stream.
- The tenant can't change on an open connection.
TenantInfocan only be set once per scope, so to switch tenants the client must open a new connection. - The connection request must resolve the tenant. Make sure the route the connection uses goes through
UseMultiTenant<TId>()and isn't excluded from resolution. Choose a strategy that works for that request: browsers can't add custom headers when opening a WebSocket, so the Header Strategy usually won't work there. Route, host, base path, or claim strategies work, as does a query string read withWithHttpContextStrategy(). - Transports that use several requests need the same tenant on each. Some transports, such as SignalR's long polling fallback, spread one logical connection across several HTTP requests, each with its own tenant scope. Make sure every one of those requests resolves to the same tenant.
- Work outside the request doesn't see the scope. Code that runs outside the connection's request, such as a
hosted service sending messages through an
IHubContext, has no ambient tenant scope. Begin one yourself withIServiceProvider.BeginTenantScope(tenantInfo)before using tenant-aware services there (see .NET Generic Host Integration).
ASP.NET Core Strategies
The following strategies are provided by Finbuckle.MultiTenant.AspNetCore and work with the ASP.NET Core
request pipeline. See MultiTenant Strategies for full documentation of all strategies, including
general-purpose strategies available in the base package.
| Strategy | Description |
|---|---|
| Host Strategy | Uses subdomains or other host segments to identify the tenant. |
| Route Strategy | Uses a route parameter (e.g. {__tenant__}) in the URL path. |
| Base Path Strategy | Uses the first URL path segment as the tenant identifier. |
| Header Strategy | Uses an HTTP request header to identify the tenant. |
| Claim Strategy | Uses a claim on the authenticated user's principal. |
| Session Strategy | Uses the ASP.NET Core session to store the resolved identifier. |
| HttpContext Strategy | Uses a delegate accepting HttpContext for custom logic. |
| Remote Authentication Callback Strategy | Used internally by per-tenant authentication for remote auth flows. |
Getting the Current Tenant in ASP.NET Core
HttpContext Extension Members
The following extension members are available on HttpContext for web apps:
GetTenantContext<TTenantInfo, TId>
Returns the ITenantContext<TTenantInfo, TId> instance for the current request. This is the preferred way to
access the current tenant in ASP.NET Core because it always reflects the state set by the middleware, even
in post-endpoint processing.
var tenantInfo = HttpContext.GetTenantContext<TenantInfo, string>().TenantInfo;
if (tenantInfo != null)
{
var tenantId = tenantInfo.Id;
var identifier = tenantInfo.Identifier;
}
TenantContext<TId>()
Returns the ITenantContext<TId> instance for the current request. This is useful when code does not need
the concrete tenant info type, or when working with shared infrastructure that only depends on ITenantInfo<TId>.
var tenantContext = HttpContext.TenantContext<string>();
if (tenantContext.IsResolved)
{
var tenantId = tenantContext.TenantInfo?.Id;
}
GetTenantInfo<TTenantInfo, TId>
A convenience shorthand for GetTenantContext<TTenantInfo, TId>().TenantInfo. Returns the current TTenantInfo
instance, or null if no tenant was resolved.
var tenantInfo = HttpContext.GetTenantInfo<TenantInfo, string>();
if (tenantInfo != null)
{
Console.WriteLine($"Current tenant: {tenantInfo.Identifier}");
}
TenantInfo<TId>()
A convenience shorthand for TenantContext<TId>().TenantInfo. Returns the current ITenantInfo<TId> instance, or null if
no tenant was resolved.
var tenantInfo = HttpContext.TenantInfo<string>();
if (tenantInfo != null)
{
Console.WriteLine($"Current tenant: {tenantInfo.Identifier}");
}
SetTenantInfo<TTenantInfo, TId>
For most cases the middleware sets the TenantInfo automatically and this method is not needed. Use only if
explicitly overriding the TenantInfo set by the middleware.
Sets the current tenant to the provided TenantInfo on the request's ITenantContext<TTenantInfo, TId>.
Important:
TenantInfocan only be set once. Attempting to callSetTenantInfoafter the tenant has already been resolved will throw aMultiTenantException. UseTrySetTenantInfoif you need to conditionally set the tenant only when none has been resolved yet.
var newTenantInfo = new TenantInfo { Id = "new-id", Identifier = "new-identifier" };
HttpContext.SetTenantInfo(newTenantInfo);
// This will be the new tenant.
var tenant = HttpContext.GetTenantContext<TenantInfo, string>().TenantInfo;
TrySetTenantInfo<TTenantInfo, TId>
Sets the current tenant only if one has not already been resolved. This is useful when your code may be called
from multiple paths and you want to avoid the exception thrown by SetTenantInfo when a tenant already exists.
var fallbackTenant = new TenantInfo { Id = "default", Identifier = "default" };
// Only takes effect if no tenant was resolved by the middleware.
HttpContext.TrySetTenantInfo<TenantInfo, string>(fallbackTenant);
For dependency injection-based access to the current tenant outside of a controller or middleware, see
ITenantContextin Configuration and Usage.
Bypassing Tenant Resolution
Bypassing prevents the middleware from performing tenant resolution for a request entirely and passes it directly to the next middleware in the pipeline.
Bypassing runs before tenant resolution. Short circuiting runs after resolution.
Exclude Specific Endpoints
For scenarios where individual, known endpoints should never trigger tenant resolution, annotate them at registration time.
Using the IEndpointConventionBuilder extension ExcludeFromMultiTenantResolution:
var app = builder.Build();
// Exclude OpenApi endpoints.
app.MapOpenApi()
.ExcludeFromMultiTenantResolution();
// Exclude a specific endpoint.
app.MapGet("/oops", () => "Oops! An error happened.")
.ExcludeFromMultiTenantResolution();
// Exclude a group of endpoints.
app.MapGroup("api/v{version:apiVersion}/dashboard")
.ExcludeFromMultiTenantResolution();
// Exclude static asset endpoints.
app.MapStaticAssets()
.ExcludeFromMultiTenantResolution();
app.Run();
Using the ExcludeFromMultiTenantResolutionAttribute attribute on controllers and action methods:
// Exclude an entire controller.
[ExcludeFromMultiTenantResolution]
public class DashboardController : Controller
{
// Or exclude just a specific action.
[ExcludeFromMultiTenantResolution]
public ActionResult Index()
{
return View();
}
}
Bypass When No Endpoint Is Matched
Call BypassWhenEndpointNotResolved() after AddMultiTenant<TTenantInfo, TId> to skip tenant resolution
entirely when the current request has no matched endpoint. This is especially useful when requests arrive
that do not match any route.
This bypass does not require the route strategy.
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithRouteStrategy()
.WithConfigurationStore()
.BypassWhenEndpointNotResolved();
Bypass When a Custom Condition Is Met
Use BypassWhen() to bypass resolution based on any condition derived from HttpContext:
// Bypass resolution for health check requests.
builder.Services.AddMultiTenant<TenantInfo,string>()
.WithRouteStrategy()
.WithConfigurationStore()
.BypassWhen(options =>
{
options.Predicate = ctx => ctx.Request.Path.StartsWithSegments("/health");
});
Short Circuiting
The MultiTenantMiddleware can be configured to short circuit a request pipeline when no tenant is found
or when some custom condition is met.
Short-circuiting the request pipeline does not return an HTTP error code — it simply stops calling further middleware.
Short Circuit When Tenant Not Resolved
Call ShortCircuitWhenTenantNotResolved() after AddMultiTenant<TTenantInfo, TId> to halt further processing
when no tenant can be found. An overload accepts a URI to redirect to when no tenant is found.
If you short circuit when tenant not resolved, and you have endpoints that do not require a tenant, then excluding those endpoints becomes a necessity; otherwise they would never be reached.
// Simply short circuit the request, ending request handling.
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithHostStrategy()
.WithConfigurationStore()
.ShortCircuitWhenTenantNotResolved();
// Short circuit and redirect to a specific Uri.
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithHostStrategy()
.WithConfigurationStore()
.ShortCircuitWhenTenantNotResolved(new Uri("/tenant/notfound", UriKind.Relative));
Short Circuit When a Custom Condition Is Met
Use the ShortCircuitWhen() extension on MultiTenantBuilder<TTenantInfo, TId> for advanced short-circuiting:
// Advanced short circuiting: if tenant not resolved.
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithHostStrategy()
.WithConfigurationStore()
.ShortCircuitWhen(config =>
{
config.Predicate = context => !context.IsResolved;
});
// Including a redirect.
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithHostStrategy()
.WithConfigurationStore()
.ShortCircuitWhen(config =>
{
config.Predicate = context => !context.IsResolved;
config.RedirectTo = new Uri("/tenant/notfound", UriKind.Relative);
});
Per-Tenant Authentication
MultiTenant provides built-in support for isolating tenant authentication so that login sessions are scoped to the current tenant. This includes per-tenant cookie validation, challenge schemes, login/logout paths, and OpenID Connect settings.
Authentication is configured by calling WithPerTenantAuthentication() after AddMultiTenant<TTenantInfo, TId>():
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie()
.AddOpenIdConnect();
builder.Services.AddMultiTenant<TenantInfo, string>()
.WithRouteStrategy()
.WithConfigurationStore()
.WithPerTenantAuthentication();
Place
UseMultiTenant<TId>()beforeUseAuthentication()so the tenant is resolved before authentication runs.
See Per-Tenant Authentication for full details, including OpenID Connect configuration, per-tenant cookie names, and JWT bearer customization.
Per-Tenant Options
MultiTenant integrates with the standard ASP.NET Core Options pattern so that options can be configured
differently for each tenant. Once the middleware sets the tenant for a request, IOptions<T>,
IOptionsSnapshot<T>, and IOptionsMonitor<T> all return the correct per-tenant values automatically.
Any options class used by ASP.NET Core or your own code can be customized per tenant with minimal changes.
IOptionsMonitor<T>is registered as a scoped service in MultiTenant (unlike standard .NET where it is a singleton), but it preserves the same change-notification and cache-invalidation behavior.
builder.Services.ConfigurePerTenant<MyOptions, TenantInfo>((options, tenantInfo) =>
{
options.Setting = tenantInfo.Identifier;
});
See Per-Tenant Options for full details.
Data Isolation
Entity Framework Core
MultiTenant can automatically filter EF Core queries by tenant using a global query filter. This removes
the need to add WHERE TenantId = ... clauses throughout your app. Use AddMultiTenantDbContext<T, TId>() for
scoped contexts or AddPooledMultiTenantDbContext<T, TId>() for high-throughput pooled contexts:
builder.Services.AddMultiTenantDbContext<AppDbContext, string>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
See Data Isolation with Entity Framework Core for full details.
ASP.NET Core Identity
MultiTenant has dedicated support for data isolation when using ASP.NET Core Identity with EF Core, including multi-tenant identity context base classes and support for passkeys (WebAuthn).
See Data Isolation with ASP.NET Core Identity for full details.
Important Considerations
- Middleware ordering matters. Place
UseMultiTenant<TId>()beforeUseAuthentication(),UseAuthorization(), and any middleware that reads per-tenant options or services. TenantInfocan only be set once per request. The middleware sets it early in the pipeline. If you need to choose the tenant yourself, callHttpContext.SetTenantInfo<TTenantInfo, TId>()orTrySetTenantInfo<TTenantInfo, TId>()in middleware placed beforeUseMultiTenant<TId>(); the middleware then skips resolution and keeps that tenant.ITenantContextis ambient. The current tenant is held in anAsyncLocal, not a scoped DI service. A fresh ambient tenant scope begins at the start of each request's pipeline (see The Per-Request Ambient Tenant Scope) and the middleware sets the resolved tenant on it, so each request sees its own tenant automatically.- Long-lived connections keep the tenant they opened with. WebSockets, SignalR, server-sent events, and streaming gRPC calls keep their request's tenant for the life of the connection. See Long-Lived Connections.
IOptionsMonitor<T>is scoped in MultiTenant. Do not capture it in a singleton service.- Not all strategies work for all scenarios. The Claim Strategy needs
authentication middleware to run first. The Route Strategy requires
UseRouting()beforeUseMultiTenant<TId>(). - Per-tenant options require
ITenantContext.TenantInfoto be set. If tenant resolution fails (no strategy finds an identifier or no store matches), theTenantInfowill be null and default (non-tenant) options will be used.
See Also
- Configuration and Usage — registration and resolver details
- Core Concepts —
ITenantContext<TId>and the ambient tenant scope - .NET Generic Host Integration — using MultiTenant in non-web apps
- Per-Tenant Authentication — full authentication setup
- Per-Tenant Options — options customization
- EF Core Data Isolation — shared and separate database patterns