Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Multi-tenant User Login using ASP.NET Identity

Tags:

I'm developing a Multi-tenant solution and I want to use the latest ASP.NET Identity framework specifically the Entity Framework implementation.

Basically I need to allow two users to have the same username, though it must be unique within a tenant, I'd also like to make use of external sign-ins such as Twitter, Microsoft, Facebook and Google.

From what I can tell, the framework was not designed with multi-tenancy in mind, though it does seem to allow extensibility. I'd prefer to make use of as much existing components as possible, though at the moment I think I'm going to have to abandon the Entity Framework implementation, and code it all myself.

Has anyone else tried this and how much have they had to code themselves.

like image 545
James Skimming Avatar asked Nov 17 '13 22:11

James Skimming


People also ask

How do you implement multi tenants?

We can implement multi-tenancy using any of the following approaches: Database per Tenant: Each Tenant has its own database and is isolated from other tenants. Shared Database, Shared Schema: All Tenants share a database and tables. Every table has a Column with the Tenant Identifier, that shows the owner of the row.

What is Multitenancy in asp net core?

What is multi-tenancy? At its core, multi-tenancy is an architecture where one codebase serves multiple customers while maintaining data isolation. To customers, it feels like they have their own copy of the software running, while the application really is just one deployment.

What is multi-tenant application example?

Examples of large multitenant applications are Microsoft 365, Outlook.com, and visualstudio.com. From an application provider's perspective, the benefits of multitenancy mostly relate to operational and cost efficiencies.

Can we have multiple tenant ID in Azure?

So, all the user accounts and devices of an organization reside in a common Azure AD tenant/instance. Does subscription belong to only 1 tenant? No. Same subscription can have multiple tenants.


2 Answers

Yeah we explicitly left this as an extensibility scenario. You should be able to do this via overriding UserManager properties and implementing your own custom IUserStore. Although you might be able to extend the EF UserStore potentially as well and just add a tenantId to your user.

like image 22
Hao Kung Avatar answered Oct 06 '22 01:10

Hao Kung


I've now got a working solution that I've shared in a GitHub repository:

https://github.com/JSkimming/AspNet.Identity.EntityFramework.Multitenant

The extensibility required to support multi-tenancy is not possible with the 1.0.0 release of Microsoft.AspNet.Identity.EntityFramework (at least not without a lot of custom work), but is available in the 1.1 alpha release currently available through the Nightly ASP.NET Web Stack NuGet Packages.

I've decided to release what I've produced as a NuGet package AspNet.Identity.EntityFramework.Multitenant, as stated it currently requires the Nightly ASP.NET Web Stack NuGet Packages.

I've provided two simple sites that make use the package to allow for multi-tenancy:

  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.
  2. 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.

I plan to update the solution as the 1.1 version of ASP.NET Identity is released.

I hope others find some use in the nuget package I've released, any feedback is more than welcome and please raise any issues you find.


Update

The solution has now been updated to use the 2.0 release of Microsoft.AspNet.Identity.EntityFramework.

like image 51
James Skimming Avatar answered Oct 06 '22 02:10

James Skimming