如何在asp.net-core中实现多租户功能 [英] how to implement Multi-Tenant functionality in asp.net-core

查看:542
本文介绍了如何在asp.net-core中实现多租户功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Asp.net Core应用程序,我希望能够允许多个/不同的Tenant(Client)访问同一应用程序,但使用不同的url.我有所有租户(客户)的通用数据库. 因此,这是我要在域中托管我的应用程序的主要部分,例如……www.myapplication.com然后允许不同的Tenant(client)使用

I have an Asp.net Core application I want to be able to allow multiple/ different Tenant(Client)to access the same application but using different url's. I have common database for all tenant(client). So It is the main part I want to host my application in a domain say... www.myapplication.com then allow different Tenant(client) to access the same application using

1.www.TenantOne.myapplication.com

1.www.TenantOne.myapplication.com

2.www.TenanatTwo.myapplication.com.

2.www.TenanatTwo.myapplication.com.

3.www.{TENANCY_NAME} .myapplication.com

3.www.{TENANCY_NAME}.myapplication.com

我找不到有关如何执行此操作的任何信息,因此被卡住了. 怎么做?请提供验证码.谢谢.

I can't find any info on how to do this and I'm stuck. How to do it? Please provide the code. Thanks.

推荐答案

我相信您正在寻找的方法与下面网址中的文章类似.

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

https://dotnetthoughts.net/building-多租户web应用与aspnet核心/

在其中,作者将请求的URL拆分为由地址中的点分隔的字符串数组.然后将变量子域"设置为该数组的第一个元素.在您的问题中,看起来您可能想使用数组中的第二个元素,但是您明白了.

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

如何使用这些数据取决于您.本文的作者创建了一个filter属性,但是有很多可能性,例如将承租人名称作为参数传递给服务函数.

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.

这篇关于如何在asp.net-core中实现多租户功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆