设置一个路由{租户} / {控制器} / {行动} / {ID}的ASP.NET MVC? [英] Setup a route {tenant}/{controller}/{action}/{id} with ASP.NET MVC?

查看:107
本文介绍了设置一个路由{租户} / {控制器} / {行动} / {ID}的ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个多租户ASP.NET MVC应用程序。理想情况下,应用程序将与路由{租户} / {控制器} / {行动} / {ID} ,每个租户重新presenting应用程序的逻辑实例(简单独立的多用户帐户)

I would like to setup a multi-tenant ASP.NET MVC app. Ideally, this app would have a route with {tenant}/{controller}/{action}/{id}, each tenant representing an logical instance of the app (simply independent multi-user accounts)

的细粒度细节如何做到这一点还比较我不清楚。提供任何指导建立这种多租户模式与ASP.NET MVC?

The fine grained details how do that are still quite unclear to me. Any guide available to setup such multi-tenant scheme with ASP.NET MVC?

推荐答案

我目前正在使用ASP.Net MVC一个类似的项目,表单验证和会员/角色/配置文件的SQL供应商。这里是我采取的方法:

I am currently working on a similar project using ASP.Net MVC, Forms Authentication and the SQL providers for Membership/Roles/Profile. Here is the approach I am taking:


  1. 注册为`{租户} / {控制器} / {行动} / {ID}

  1. Register the default route as `{tenant}/{controller}/{action}/{id}

更改附带标准的MVC模板FormsAuthenticationService的默认行为。它应该设置身份验证票据的的UserData包括承租人的名字(从你的路线)。

Change the default behavior of the FormsAuthenticationService that comes with the standard MVC template. It should set the UserData of the authentication ticket to include the tenant name (from your route).

public void SignIn(string userName, bool createPersistentCookie, string tenantName)
{
    var ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30),
                                               createPersistentCookie, tenantName);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
    HttpContext.Current.Response.AppendCookie(cookie);
}


  • 在您的Global.asax文件做一些租户的安全检查,并允许租户之间的用户partioning在一个会员数据库

  • In your global.asax file to do some tenant security checking and allow partioning of users between tenants in one membership database

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        //Since this method is called on every request
        //we want to fail as early as possible
        if (!Request.IsAuthenticated) return;
        var route = RouteTable.Routes.GetRouteData(new HttpContextWrapper(Context));
        if (route == null || route.Route.GetType().Name == "IgnoreRouteInternal") return;
        if (!(Context.User.Identity is FormsIdentity)) return;
        //Get the current tenant specified in URL 
        var currentTenant = route.GetRequiredString("tenant");
        //Get the tenant that that the user is logged into
        //from the Forms Authentication Ticket
        var id = (FormsIdentity)Context.User.Identity;
        var userTenant = id.Ticket.UserData;
        if (userTenant.Trim().ToLower() != currentTenant.Trim().ToLower())
        {
            //The user is attempting to access a different tenant
            //than the one they logged into so sign them out
            //an and redirect to the home page of the new tenant
            //where they can sign back in (if they are authorized!)
            FormsAuthentication.SignOut();
            Response.Redirect("/" + currentTenant);
            return;
        }
        //Set the application of the Sql Providers 
        //to the current tenant to support partitioning
        //of users between tenants.
        Membership.ApplicationName = currentTenant;
        Roles.ApplicationName = currentTenant;
        ProfileManager.ApplicationName = currentTenant;
    }
    


  • 分区的每个租户的数据。这里有两个选项:

  • Partition each tenants data. Here are two options:

    4a上。使用单独的数据库为每个租户。这为您的租户最好的数据安全。在共享会员数据库,添加一个独特的appid锁定式设计,每个租户一个表并使用这个表来存储和检索基于当前租户的连接字符串。

    4a. Use a separate database for each tenant. This provides the best data security for your tenants. In the shared membership database, add a table that is keyed on unique appid for each tenant and use this table to store and retrieve the connection string based on the current tenant.

    4b上。 Store上唯一租户ID在一个数据库和关键每个表的所有数据。这提供了略显不足的数据安全为您的租户,但仅使用一个SQL Server许可证。

    4b. Store all data in one database and key each table on the unique tenant id. This provides slightly less data security for your tenants but uses only one SQL Server license.

    这篇关于设置一个路由{租户} / {控制器} / {行动} / {ID}的ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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