NetCoreBackend.NArchitecture.Core.MultiTenancy 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NetCoreBackend.NArchitecture.Core.MultiTenancy --version 1.0.0
                    
NuGet\Install-Package NetCoreBackend.NArchitecture.Core.MultiTenancy -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetCoreBackend.NArchitecture.Core.MultiTenancy" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetCoreBackend.NArchitecture.Core.MultiTenancy" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="NetCoreBackend.NArchitecture.Core.MultiTenancy" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetCoreBackend.NArchitecture.Core.MultiTenancy --version 1.0.0
                    
#r "nuget: NetCoreBackend.NArchitecture.Core.MultiTenancy, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NetCoreBackend.NArchitecture.Core.MultiTenancy@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetCoreBackend.NArchitecture.Core.MultiTenancy&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=NetCoreBackend.NArchitecture.Core.MultiTenancy&version=1.0.0
                    
Install as a Cake Tool

Core.MultiTenancy

SaaS uygulamaları için multi-tenant altyapısı. Tenant tespitini JWT claim, HTTP header ve subdomain üzerinden sırayla yapar.

Tenant Tespiti (Öncelik Sırası)

1. JWT claim: "tenant_id"          → En güvenli, token doğrulandıktan sonra gelir
2. HTTP Header: X-Tenant-ID        → API client'lar ve geliştirme ortamı için
3. Subdomain: acme.yourapp.com     → Production SaaS URL yapısı için

Bileşenler

Bileşen Açıklama
Tenant Tenant kaydı entity'si (name, identifier/slug, domain, plan, defaultLocale, isActive)
ITenantContext Mevcut tenant'ı okumak için DI'a inject edilen interface
TenantContext Scoped, request başına sıfırlanan ITenantContext implementasyonu
ITenantService Uygulamada implement edilmesi gereken tenant lookup interface'i
TenantEntitySetter ITenantEntitySetter implementasyonu — Add işlemlerinde TenantId'yi otomatik set eder
TenantMiddleware Her request'te tenant'ı çözen middleware
TenantClaimTypes Claim key sabitleri (tenant_id, is_super_admin, is_impersonating)

Kurulum

// Program.cs
builder.Services.AddMultiTenancy();
// → ITenantContext, TenantContext ve ITenantEntitySetter otomatik kaydedilir

builder.Services.AddScoped<ITenantService, YourTenantService>();

app.UseAuthentication();
app.UseMultiTenancy();   // UseAuthentication'dan SONRA gelmeli
app.UseAuthorization();

Middleware sırası neden önemli? TenantMiddleware'in 1. öncelik kaynağı JWT'deki tenant_id claim'idir. Bu claim ancak UseAuthentication() çalıştıktan sonra HttpContext.User üzerinden okunabilir. Sıralama ters olursa User.Claims boş kalır, middleware doğrudan header/subdomain fallback'lerine düşer ve oturum açmış kullanıcılar bile yanlış tenant'a (veya hiçbir tenant'a) yönlendirilir.

AddMultiTenancy() şunları kaydeder:

  • TenantContext (scoped)
  • ITenantContextTenantContext (scoped)
  • ITenantEntitySetterTenantEntitySetter (scoped) — ayrıca kaydetmeye gerek yoktur

Tenant Entity

public class Tenant : Entity<Guid>
{
    public string Name { get; set; }
    public string Identifier { get; set; }   // slug: "acme"
    public string? Domain { get; set; }
    public bool IsActive { get; set; }
    public TenantPlanType PlanType { get; set; }
    public string? DefaultLocale { get; set; }  // "tr", "de" — Accept-Language yoksa fallback
}

DefaultLocale: Client Accept-Language header'ı göndermediğinde LocalizationMiddleware bu değeri fallback olarak kullanır.

Identifier unique olmalı. Framework code-level uniqueness check yapmıyor; DB constraint'i consuming app'in DbContext konfigürasyonunda eklenmeli:

modelBuilder.Entity<Tenant>().HasIndex(t => t.Identifier).IsUnique();
// Domain için de aynı (Domain nullable, multi-tenant)
modelBuilder.Entity<Tenant>()
    .HasIndex(t => t.Domain)
    .IsUnique()
    .HasFilter("[Domain] IS NOT NULL");

Bu constraint olmazsa acme slug'lı iki Tenant kaydı oluşturulabilir, GetBySlugAsync belirsiz sonuç döndürür.

SuperAdmin

JWT'de is_super_admin: true ve tenant_id: null olan kullanıcı tüm tenant verilerine erişir (EF Core global filter bypass edilir). Belirli bir tenant'a geçmek için impersonation token kullanılır.

Detaylı dokümantasyon: TENANT.md

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on NetCoreBackend.NArchitecture.Core.MultiTenancy:

Package Downloads
NetCoreBackend.NArchitecture.Core.Security

Provides security utilities for hashing, encryption, JWT, authenticator, and more.

NetCoreBackend.NArchitecture.Core.Application

Provide CQRS, MediatR, Pipelines, DTOs, and more for the Application Layer.

NetCoreBackend.NArchitecture.Core.Localization.WebApi

Provide localization support for ASP.NET Web API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 93 6/9/2026
1.0.1 81 6/9/2026
1.0.0 135 6/2/2026