Multi-tenant support for ASP.NET Identity using Entity Framework
Multi-tenant support for ASP.NET Identity using Entity Framework
This library was created to solve a problem I asked on Stack Overflow, it was needed for a commercial project, this part has been open sourced in the hope that it may receive improvements and future support by receiving wider usage.
It is available to download as a NuGet package.
Additionally a Continious integration Build NuGet feed is also provided by AppVeyor https://ci.appveyor.com/nuget/aspnet-identity-entityframewor-avbe23sgoogy Add this feed to your NuGet Configuration Settings to include the CI build, which includes yet to be accepted pull requests.
To use this library the
TenantIdproperty on the
MultitenantUserStoreneeds to be set for the relevant tenant. How and when this is set is determined by the application.
Thereafter, the
UserManager(which has a dependency on the
UserStore) can be used and it will only find and update users for the specified tenant.
The intention is to provide a means to allow for identical users (uniquely keyed upon email/username/external login) to register and authenticate multiple times under different tenants, but still remaining unique within a tenant, all within the same database and entity context.
The two entities that have been extended to support multi tenancy are
IdentityUserand
IdentityUserLogin, both of which have had the
TenantIdproperty added.
Following the same patterns of Microsoft ASP.NET Identity EntityFramework there is both a generic and non generic equivalent proved:
MultitenantIdentityUserand
MultitenantIdentityUserLoginif you want the
TenantIdto be a string
MultitenantIdentityUserand
MultitenantIdentityUserLoginif you want custom
TenantId.
UserStorehas been extended to have a
TenantIdproperty and to override the necessary methods to implement multi-tenancy. The
TenantIdneeds to be specified directly otherwise an
InvalidOperationExceptionwill be thrown. See the
MultitenantUserStorefor more details.
IdentityDbContexthas been extended to add the necessary validation and model creation to implement multi-tenancy. See the
MultitenantIdentityDbContextfor more details.
Two simple sites that make use the package to allow for multi-tenancy are provided as example: