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 TenantId
property on the MultitenantUserStore
needs 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 IdentityUser
and IdentityUserLogin
, both of which have had the TenantId
property added.
Following the same patterns of Microsoft ASP.NET Identity EntityFramework there is both a generic and non generic equivalent proved:
MultitenantIdentityUser
and MultitenantIdentityUserLogin
if you want the TenantId
to be a stringMultitenantIdentityUser<TKey, TTenantKey, TLogin, TRole, TClaim>
and MultitenantIdentityUserLogin<TKey, TTenantKey>
if you want custom TenantId
.The UserStore<TUser>
has been extended to have a TenantId
property and to override the necessary methods to implement multi-tenancy. The TenantId
needs to be specified directly otherwise an InvalidOperationException
will be thrown. See the MultitenantUserStore<TUser, TRole, TKey, TTenantKey, TUserLogin, TUserRole, TUserClaim>
for more details.
Lastly the IdentityDbContext<TUser>
has been extended to add the necessary validation and model creation to implement multi-tenancy. See the MultitenantIdentityDbContext<TUser, TRole, TKey, TTenantKey, TUserLogin, TUserRole, TUserClaim>
for more details.
Two simple sites that make use the package to allow for multi-tenancy are provided as example: