/AspNet.Identity.EntityFramework.Multitenant

Multi-tenant support for ASP.NET Identity using Entity Framework

Primary LanguageC#Apache License 2.0Apache-2.0

ASP.NET Identity EntityFramework Multitenant

NuGet Version NuGet Downloads Build status Latest release

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.

How to use multi-tenancy

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.

What has been multi-tenanted?

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:

  1. Use or extend MultitenantIdentityUser and MultitenantIdentityUserLogin if you want the TenantId to be a string
  2. Extend MultitenantIdentityUser<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.

Example

Two simple sites that make use the package to allow for multi-tenancy are provided as example:

  1. Vanilla Implementation which is the standard MVC5 template project that uses a string primary keys.
  • The changes necessary to provide multi-tenancy have been isolated to this commit.
  1. Integer Primary Key Implementation which is making a few more customisations to use integer primary keys
  • The changes necessary to provide multi-tenancy have been isolated to this commit.