简体   繁体   English

windows azure asp.net MVC2中的多租户

[英]Multi-Tenant in windows azure asp.net MVC2

Anyone knows how can i start to develop a multitenant site in MVC2, in a way it run on Windows Azure? 任何人都知道如何开始在MVC2中开发多租户网站,它在Windows Azure上运行?

I search a lot about this question, and i allways find theoric explanations, everybody says it can be easily done, but i dont find any sample... 我搜索了很多关于这个问题,我总是找到理论解释,每个人都说它很容易完成,但我找不到任何样本......

Can someone explain me where to start? 有人可以解释我从哪里开始?

Thanks, João 谢谢,João

It depends on how you plan on implementing multitenancy (eg. using authorization with common urls, subdomains, custom domains, or any combination). 这取决于您计划如何实施多租户(例如,使用具有公共URL,子域,自定义域或任何组合的授权)。 But you should be able to do just about any approach with Azure and MVC2. 但是你应该能够使用Azure和MVC2做任何方法。 If you plan on using a custom domain for each tenant, versus a subdomain, you will need to be happy with using CNAME entries (not A records) to point each custom domain to Azure but that usually is not a problem. 如果您计划为每个租户使用自定义域而不是子域,则需要使用CNAME条目(而不是A记录)将每个自定义域指向Azure,但这通常不是问题。

MVC offers many extension points where you can implement multitenancy in its various flavors. MVC提供了许多扩展点,您可以在其中实现各种风格的多租户。 The main goal is to uniquely identify the user by either a login or the url. 主要目标是通过登录或URL唯一地标识用户。

We have an MVC2 application running in Azure that parses the request url to differentiate the tenant. 我们在Azure中运行了一个MVC2应用程序,它解析请求URL以区分租户。 There are many ways to do this. 有很多方法可以做到这一点。 We took the approach of extending the Controller class to provide our app with the unique tenant information so we could use it as needed to make appropriate repository calls to display the proper views etc. 我们采用扩展Controller类的方法为我们的应用程序提供唯一的租户信息,以便我们可以根据需要使用它来进行适当的存储库调用以显示正确的视图等。

Here is a sample of what a MultiTenant Controller might look like: 以下是MultiTenant Controller的外观示例:

public class MultiTenantController : Controller {
    public string TenantCode { get; set; }

    protected override void OnActionExecuting(ActionExecutingContext filterContext) {
        TenantCode = GetTenantCode(filterContext.HttpContext.Request);
    }

    private string GetTenantCode(System.Web.HttpRequestBase request) {
        string host = new RequestParser(request.Url.AbsoluteUri).Host;
        return _tenantService.GetTenantCodeByHostAddress(host);
    }
}

NOTES: 笔记:

  1. The RequestParser function above is just any implementation that knows how to parse urls in a safe manner. 上面的RequestParser函数只是知道如何以安全的方式解析URL的任何实现。
  2. _tenantService can access some kind of persistent store (Azure Tables in our case) to get the TenantCode from the host address in the url. _tenantService可以访问某种持久性存储(在我们的例子中为Azure Tables),以从url中的主机地址获取TenantCode。

All of your controllers would inherit from the above class. 所有控制器都将继承上述类。 Then, to differentiate between tenants you just refer to the TenantCode within your controller like so: 然后,为了区分租户,您只需在控制器中引用TenantCode ,如下所示:

public class HomeController : MultiTenantController {
    ...

    public ViewResult Index() {
        var vm = _homeService.GetHomePageViewModelForTenant(TenantCode);
        return View(vm);
    }
}

Using the above implementation you could serve different sites or data to urls like the following: 使用上述实现,您可以将不同的站点或数据提供给以下URL:
http://subtenant1.yourdomain.com http://subtenant1.yourdomain.com
http://subtenant2.yourdomain.com http://subtenant2.yourdomain.com
http://www.customtenantdomain.com http://www.customtenantdomain.com

Your backend store (eg. Table Storage) just needs to cross reference host names with the tenant like the table below. 您的后端存储(例如,表存储)只需要与租户交叉引用主机名,如下表所示。 In the code above GetTenantCode would access the data. 在上面的代码中, GetTenantCode将访问数据。

HostName                TenantCode
---------------------- --------------  
subtenant1              Tenant1ID  
subtenant2              Tenant2ID  
www.customtenantdomain  Tenant3ID  

For www.customtenantdomain.com to work, the tenant needs a CNAME entry for www in their DNS records for customtenantdomain.com that points to your Azure Web Role's address. 要使www.customtenantdomain.com正常工作,租户需要在其指向Azure Web角色地址的customtenantdomain.com的DNS记录中使用www的CNAME条目。

Its hugely complex and not something to be taken on lightly. 它非常复杂,不能轻易采取。 However take a look at the source code for Microsoft's Orchard project. 但是,请查看Microsoft的Orchard项目的源代码。 This has full multi-tenancy capabilities if thats what you need: http://orchard.codeplex.com/ 如果您需要,这具有完整的多租户功能: http//orchard.codeplex.com/

And they have a build that works in Azure too. 他们也有一个在Azure中运行的构建。

在本指南中,我们将介绍这方面的内容,并包含使用MVC 2 链接文本的完整示例

First , all answers are very very helpful.It's changing your decision what you want setting up your multitenancy.I mean the most important thing is Identifying all tenant in your app so there is a lot of way for solution.For example you can hold your tenant via subdomains or URL surfing.And also maybe you can store your data multitenat database. 首先,所有的答案都非常有用。它正在改变你想要设置你的多租户的决定。我的意思是最重要的是识别你的应用程序中的所有租户,所以有很多方法可以解决。例如,你可以持有你的租户通过子域或URL冲浪。也许您可以存储您的数据multitenat数据库。

There are very very helpul posts are written by Steve Morgan. 史蒂夫摩根写的非常非常有帮助的帖子。

I only help you for set startup multi- tenancy.Here are the blogs : 我只帮助你设置启动多租户。这是博客:

  1. Identifying the Tenant in Multi-Tenant Azure Applications - Part 1 识别多租户Azure应用程序中的租户 - 第1部分
  2. Identifying the Tenant in Multi-Tenant Azure Applications - Part 2 识别多租户Azure应用程序中的租户 - 第2部分
  3. Identifying the Tenant in Multi-Tenant Azure Applications - Part 3 识别多租户Azure应用程序中的租户 - 第3部分

And here are the Multi-Tenant Data Strategies for Windows Azure : 以下是Windows Azure的多租户数据策略:

  1. Multi-Tenant Data Strategies for Windows Azure – Part 1 Windows Azure的多租户数据策略 - 第1部分
  2. Multi-Tenant Data Strategies for Windows Azure – Part 2 Windows Azure的多租户数据策略 - 第2部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM