how to implement Multi-Tenant functionality in asp.net-core

孤人 提交于 2019-12-07 02:26:27

Sorry,you have to get something to start with and then come back for the people to help you with.

I would say that this is all of a domain based wild card mapping and change in your authentication logic to get the tenant id from the URL. Once you identified the tenant, you just login and then take it forward. Like you might be having a database with the tenant details like

tenant1 | tenant1.company.com | guid-ofthe-tenant | etc...

Once you get the URL, you lookup in the above table and get the tenant code and then you choose the login mode and then proceed.

In case you have tried something yet, we would be happy to point you if it does not work yet.

As Saravanan suggested these types of questions don't belong here on SO. To get you started, I suggest you start looking if there are any frameworks such as SaaSKit available to add a multi tenancy layer to the pipeline.

The essential part is to know where each request comes from. Using subdomains is a good way to achieve that and middleware is a good place to 'identify' your tenant. You could have a database to persist the tenants but the implementation is entirely up to you. I also wrote a little article on the subject. Although it isn't ASP.NET Core, the principles still apply.

The approach I believe you are looking for is similar to the article at the url below.

https://dotnetthoughts.net/building-multi-tenant-web-apps-with-aspnet-core/

In it, the author splits the requesting URL into an array of strings delimited by the dot in the address. The variable 'subdomain' is then set to the first element of that array. In your question, it looks like you may want to use the second element in the array, but you get the idea.

var fullAddress = actionExecutingContext.HttpContext?.Request?
     .Headers?["Host"].ToString()?.Split('.');

var subdomain = fullAddress[0];
//do something, get something, return something

How you use this data is up to you. The author of the article created a filter attribute, but there are many possibilities such as passing the tenant name as a parameter to a service function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!