{"id":24750848,"url":"https://github.com/czioutas/multitenancy","last_synced_at":"2026-02-10T18:31:08.065Z","repository":{"id":274509508,"uuid":"923148728","full_name":"czioutas/multitenancy","owner":"czioutas","description":"Easy-to-use library that adds tenant isolation to ASP.NET Core APIs. Features automatic context handling, built-in middleware, database-level isolation, and Identity integration. ","archived":false,"fork":false,"pushed_at":"2025-01-31T18:23:35.000Z","size":80,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-12T07:13:34.452Z","etag":null,"topics":["csharp","dotnet","entityframework"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/czioutas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-27T18:13:18.000Z","updated_at":"2025-01-31T18:23:40.000Z","dependencies_parsed_at":"2025-01-27T19:28:54.422Z","dependency_job_id":"9a04d9bf-3ccb-40e4-aeb5-021488448267","html_url":"https://github.com/czioutas/multitenancy","commit_stats":null,"previous_names":["czioutas/multitenancy"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/czioutas/multitenancy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czioutas%2Fmultitenancy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czioutas%2Fmultitenancy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czioutas%2Fmultitenancy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czioutas%2Fmultitenancy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czioutas","download_url":"https://codeload.github.com/czioutas/multitenancy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czioutas%2Fmultitenancy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29311068,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-10T17:48:59.043Z","status":"ssl_error","status_checked_at":"2026-02-10T17:45:37.240Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","entityframework"],"created_at":"2025-01-28T09:09:06.417Z","updated_at":"2026-02-10T18:31:07.838Z","avatar_url":"https://github.com/czioutas.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Czioutas.Multitenancy\n\n[![Test and Publish](https://github.com/czioutas/multitenancy/actions/workflows/publish.yml/badge.svg)](https://github.com/czioutas/multitenancy/actions/workflows/publish.yml)\n[![NuGet](https://img.shields.io/nuget/v/Czioutas.Multitenancy.svg)](https://www.nuget.org/packages/Czioutas.Multitenancy/)\n[![NuGet](https://img.shields.io/nuget/dt/Czioutas.Multitenancy.svg)](https://www.nuget.org/packages/Czioutas.Multitenancy/)\n\n\nA lightweight, flexible multi-tenancy library for .NET applications that makes it easy to add tenant isolation to your ASP.NET Core APIs using Entity Framework Core.\n\n## Features\n\n- Easy integration with ASP.NET Core and Entity Framework Core\n- Automatic tenant context handling through middleware\n- Built-in tenant isolation at the database level\n- Support for tenant-aware entities\n- Flexible tenant identification strategies\n- Built-in tenant management API\n- Exception handling specific to tenant operations\n- Identity integration with custom tenant support\n\n## Installation\n\nInstall the package via NuGet:\n\n```bash\ndotnet add package Czioutas.Multitenancy\n```\n\n## Quick Start\n\n1. Add tenant configuration in your `Program.cs`:\n\n```csharp\nbuilder.Services.AddMultiTenancy\u003cYourDbContext\u003e(options =\u003e\n{\n    options\n        .WithDbContext\u003cYourDbContext\u003e()\n        .WithUser\u003cYourUser\u003e()\n        .WithRole\u003cYourRole\u003e()\n        .WithCurrentUserProvider(sp =\u003e \n        {\n            // Your logic to get current user ID\n            return userId;\n        })\n        .WithCurrentUserTenantProvider(sp =\u003e\n        {\n            // Your logic to get current tenant ID\n            return tenantId;\n        });\n});\n```\n\n2. Add the middleware in your application pipeline:\n\n```csharp\napp.UseMultiTenancy();\n```\n\n3. Make your entities tenant-aware by inheriting from `TenantAwareEntity`:\n\n```csharp\npublic class YourEntity : TenantAwareEntity\n{\n    public string Name { get; set; }\n    // Your entity properties\n}\n```\n\n## Configuration Options\n\n### Tenant Identification\n\nYou can identify tenants through:\n- Custom provider functions defined in your configuration\n- HTTP Header \"X-Tenant-Id\" (used as default and fallback behavior)\n\n### Database Context Setup\n\nYour DbContext should inherit from `TenantIdentityDbContext`:\n\n```csharp\npublic class YourDbContext : TenantIdentityDbContext\u003cYourUser, YourRole, Guid\u003e\n{\n    public YourDbContext(\n        DbContextOptions options,\n        IRequestTenant requestTenant)\n        : base(options, requestTenant)\n    {\n    }\n}\n```\n\n## Exception Handling\n\nThe library includes several specialized exceptions:\n- `TenantNotFoundException`\n- `TenantAlreadyExistsException`\n- `TenantOperationException`\n\n## API Reference\n\n### ITenantService\n\n```csharp\npublic interface ITenantService\n{\n    Task\u003cTenantModel\u003e CreateAsync(string tenantIdentifier);\n    Task\u003cTenantModel\u003e FindByIdentifierAsync(string tenantIdentifier);\n    Task\u003cTenantModel\u003e GetAsync(Guid tenantId);\n    Task\u003cTenantModel\u003e GetAsync(string tenantIdentifier);\n    string GetRandomIdentifier();\n}\n```\n\n### IRequestTenant\n\n```csharp\npublic interface IRequestTenant\n{\n    Guid TenantId { get; }\n    void SetTenantId(Guid tenantId);\n}\n```\n\n## Advanced Usage\n\n### Custom Tenant Resolution\n\n```csharp\nbuilder.Services.AddMultiTenancy\u003cYourDbContext\u003e(options =\u003e\n{\n    options.WithCurrentUserTenantProvider(sp =\u003e\n    {\n        var httpContext = sp.GetRequiredService\u003cIHttpContextAccessor\u003e().HttpContext;\n        var user = httpContext?.User;\n        \n        // Your custom tenant resolution logic\n        return tenantId;\n    });\n});\n```\n\n### Query Filtering\n\nTenant isolation is automatically applied to all entities implementing `ITenantAwareEntity`. The library ensures that queries only return data belonging to the current tenant.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczioutas%2Fmultitenancy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczioutas%2Fmultitenancy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczioutas%2Fmultitenancy/lists"}