简体   繁体   English

扩展SimpleMembership以支持多租户

[英]Extending SimpleMembership to support Multi-Tenancy

I have a multi-tenant app that's using SimpleMembership. 我有一个使用SimpleMembership的多租户应用程序。 Each User in my system is linked to one or more Tenants. 我系统中的每个用户都链接到一个或多个租户。 I've extended Users simply by adding the required fields to the User model (and Tenants): 我只是通过向User模型(和租户)添加必填字段来扩展用户:

public class User{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public virtual ICollection<Tenant> Tenants { get; set; }
}

This is working great. 这很有效。 But now I'd like to have Roles be tenant-specific, where some role types MUST have a TenantId defined. 但是现在我想让Roles成为特定于租户的,其中一些角色类型必须定义TenantId。 This isn't as simple as the user problem, as each of the following will be affected: 这不像用户问题那么简单,因为以下每个都会受到影响:

Adding Roles, Checking Roles: 添加角色,检查角色:

 if (!Roles.Privider.RoleExists("Moderator"))  // I now want to include TenantId here
        {
            roles.CreateRole("Moderator"); // and here
        }

Assigning/Checking Roles Using Provider: 使用提供程序分配/检查角色:

    if (!Roles.IsUserInRole("Admin", "SystemAdministrator")) // and here
        {
            roles.AddUsersToRoles(new[] { "Admin" }, new[] { "SystemAdministrator" }); // and here
        }

Role Attributes: 角色属性:

 [System.Web.Http.Authorize(Roles = "SystemAdministrator")]
 public class AdminApiController : BaseApiController 

User.IsInRole: User.IsInRole:

 if (User.IsInRole("Administrator"))
            {

I'm pretty new to ASP.NET, so I'm not sure where to begin here. 我是ASP.NET的新手,所以我不知道从哪里开始。 Should I be overriding the SimpleMembership Role Provider somehow, or should I look into writing my own Role columns, classes, etc? 我应该以某种方式覆盖SimpleMembership角色提供者,还是应该考虑编写自己的角色列,类等? It would feel wrong to hand-code anything around authentication... Any pointers around this would be much appreciated. 在身份验证周围手动编码任何东西都会感觉不对...任何关于此的指针都将非常感激。

The first problem I see with implementing this is the use of the AuthorizeAttribute because attributes require constant information defined at compile time. 我实现这一点时遇到的第一个问题是使用AuthorizeAttribute,因为属性需要在编译时定义的常量信息。 With your tenant-based approach I would think which tenant to check would need to be determined during run-time. 使用基于租户的方法,我认为在运行期间需要确定哪个租户需要确定。 So the first thing I would do is take the approach described in this article on Decoupling You Security Model From The Application Model With SimpleMembership . 所以我要做的第一件事就是采用本文中描述的方法,将安全模型与使用SimpleMembership的应用程序模型解耦 Now you decorate this attribute with a resource and operation (which will be static) and you can configure what roles are assigned to the resource/operation at run-time in the database. 现在,您使用资源和操作(将是静态的)来装饰此属性,并且可以配置在数据库中在运行时为资源/操作分配的角色。 This gives you a lot more flexibility in designing your security model as you add tenants. 在添加租户时,这为您设计安全模型提供了更大的灵活性。

Changing the database model for anything but the UserProfile table in SimpleMembership is not possible ( See this QA ). 不能在SimpleMembership中更改除UserProfile表之外的任何内容的数据库模型( 请参阅此QA )。 So adding tenant ID's to roles is not possible without writing your own membership provider. 因此,如果不编写自己的成员资格提供程序,则无法将角色ID添加到角色中。 If you want to stick with using SimpleMembership one solution is to handle this in your naming convention for roles, where you include the role name and tenant name or ID. 如果您想坚持使用SimpleMembership,一种解决方案是在角色的命名约定中处理此问题,其中包括角色名称和租户名称或ID。 For example, if you have two tenants that have the admin role you would have two roles that are named "Administrator_Tenant1" and "Administrator_Tenant2". 例如,如果您有两个具有admin角色的租户,则您将拥有两个名为“Administrator_Tenant1”和“Administrator_Tenant2”的角色。 If you need to display this to any users that assign roles you could clean up the name by stripping out the tenant ID/Name for viewing. 如果您需要向分配角色的任何用户显示此信息,您可以通过剥离租户ID /名称来查看名称。

If this is a new project and you are not tied to SimpleMembership you may want to look at using at Microsoft's latest membership system called ASP.NET Identity . 如果这是一个新项目并且您不依赖于SimpleMembership,您可能希望在Microsoft最新的名为ASP.NET Identity的会员系统中使用。 This membership system replaces SimpleMembership in MVC 5. ASP.NET Identity was built to be easily customized and you should be able to change the role model. 此成员资格系统取代了MVC 5中的SimpleMembership.ASP.NET身份可以轻松定制,您应该能够更改角色模型。 There is an article on customizing ASP.NET Identity here . 这里有一篇关于自定义ASP.NET标识文章

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

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