简体   繁体   English

在多租户.net项目中实现角色的最佳方法是什么?

[英]What's the best way to implement roles in a multi-tenant .net project?

My application is .NET 4.7 Web Api with Entity Framework 6. 我的应用程序是带有实体框架6的.NET 4.7 Web Api。

This is for a software as a service tool I am building. 这是针对我正在构建的软件即服务工具。 In my application there is a many to many relationship between AspNetUser and Company. 在我的应用程序中,AspNetUser和Company之间存在多对多关系。

A user can have many companies, and a company can have many users. 一个用户可以有很多公司,而一个公司可以有很多用户。 A user needs to have a different role / set of permissions based on what company it wants to work with. 用户需要根据其与之合作的公司而具有不同的角色/权限集。 What is the best way to implement something like this? 实现这样的最佳方法是什么?

I have thought about adding a CompanyId column to the AspNetUserRoles table, so a user would have a different role for different companies it is related with. 我已经考虑过将CompanyId列添加到AspNetUserRoles表中,因此用户对于与其相关的不同公司将具有不同的角色。 I'm not sure this is a very good implementation, and it might break how authorization works so I don't want to touch it until I have a clearer understanding of how things work. 我不确定这是否是一个很好的实现,并且它可能会破坏授权的工作方式,因此在我对事情的工作方式有了更清晰的了解之前,我不希望碰它。

So far I have found this guide on resource-based authorization for multi-tenant applications. 到目前为止,我已经找到了有关多租户应用程序基于资源的授权的指南 It seems to be what I'm looking for but it is for .NET Core, and I'm not sure if it works for .NET 4.7. 这似乎是我在寻找的东西,但它是针对.NET Core的,我不确定它是否适用于.NET 4.7。

I haven't been able to find something similar for .NET 4.7 so I am reaching out to the stack overflow community. 我无法为.NET 4.7找到类似的内容,因此我正在接触堆栈溢出社区。

If you are building it from scratch I recommend to switch to ASP.NET Core and leverage of resource-based authorization. 如果您是从头开始构建它,建议您切换到ASP.NET Core,并利用基于资源的授权。

I will recommend to reconsider sharing database between tenants. 我建议重新考虑租户之间的共享数据库。 It is not exactly great idea for SaaS applications. 对于SaaS应用程序来说,这并不是一个好主意。 Unless you need to save some money and you are willing to add complexity to your code. 除非您需要节省一些钱,否则您愿意增加代​​码的复杂性。 You can read Multi-tenant SaaS database tenancy patterns . 您可以阅读多租户SaaS数据库租用模式

If you want to stick with what you have I can only recommend to introduce new class Employee that will have relationship with AspNetUser and Company. 如果您想坚持自己拥有的东西,我只能建议介绍与AspNetUser和Company有关系的新类Employee。 That is how you manage to have single user that can be tight to many companies, but on the other side Company will have only collection of Employees. 这样一来,您便可以拥有一个对许多公司都比较紧的单一用户,但另一方面,公司将仅拥有雇员集合。

According to roles you can do something similar to User-Employee-Company relationship. 根据角色,您可以执行类似于User-Employee-Company关系的操作。 As user roles needs to be managed by application owner(you), because you are setting authorization attributes on controllers and actions you only let Company admins to pick roles you defined to CompanyRole class. 由于用户角色需要由应用程序所有者(您)管理,因为您要在控制器和操作上设置授权属性,所以只允许公司管理员选择您为CompanyRole类定义的角色。 Company can have many CompanyRole with many AspNetUSerRole. 公司可以有多个AspNetUSerRole拥有多个CompanyRole。

Example: 例:

You set roles SuperAdmin[someone who has absolute control over the app and data](probably you and maybe some of your fellow workers), CompanyAdmin[someone who has absolute control over the company](probably person who created the company in your app), EmployeeAdmin[someone who has granted the access to manage employees within the company](probably person from company's HR department), EmployeeReader[someone who has granted the access to read-only employees within the company](probably anyone can read info about other employees), etc. Depends on your needs and how granular your authorization permissions are or you would like to have. 您设置了角色SuperAdmin [对应用程序和数据拥有绝对控制权的人](可能您和您的某些同事),CompanyAdmin [对公司拥有绝对控制权的人](可能在您的应用程序中创建公司的人) ,EmployeeAdmin [授予访问权限以管理公司内部员工的人员​​](可能来自公司的HR部门的人员),EmployeeReader [授予访问权限给公司内部只读员工的人员​​](可能任何人都可以阅读其他信息员工等)。取决于您的需求以及您的授权许可或希望拥有的授权许可的粒度。

Next, you let CompanyAdmin to create set of CompanyRole entries tight to his company with relationship to your roles. 接下来,让CompanyAdmin创建与他的公司紧密相关且与您的角色相关的CompanyRole条目集。 For example CompanyRole will be HumanResourcesAdmin and have relationship to your role EmployeeAdmin. 例如,CompanyRole将是HumanResourcesAdmin,并且与您的角色EmployeeAdmin有关系。

Next, CompanyAdmin needs to create new Employee and say this employee has those CompanyRoles. 接下来,CompanyAdmin需要创建新的Employee,并说该员工具有这些CompanyRoles。 You need to pair Employee with your account(if not exists he needs to register). 您需要将Employee与您的帐户配对(如果不存在,则需要注册)。 This can be done with invitation links over email you send after employee is created or CompanyAdmin clicks button Invite User. 这可以通过创建员工或CompanyAdmin单击按钮“邀请用户”后通过电子邮件发送的邀请链接来完成。

Next, after user register/login you need to take care of which company he wants to be signed in and based on it you can easily add correct roles to his identity. 接下来,在用户注册/登录后,您需要照顾他要登录的公司,并根据该公司可以轻松地为他的身份添加正确的角色。

Is it clear? 清楚吗?

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

相关问题 C#.NET实体框架多租户最佳实践 - C# .NET Entity Framework multi-tenant best practice 检查客户端应用程序发送的数据是否来自多租户应用程序中的同一租户的最有效方法是什么? - What is the most efficient way to check if data that the client app send is from the same tenant in multi-tenant app? 多租户ASP .NET应用程序中的隔离 - Isolation in a Multi-tenant ASP .NET Application Asp.Net核心多租户 - Asp.Net Core Multi-Tenant Azure 数据库水平分片是多租户 C# asp.net 应用程序的最佳解决方案 - Azure Database Horizontal Sharding the best solution for Multi-tenant C# asp.net application 在大型 .NET 项目中实现多语言/全球化的最佳方式 - Best way to implement multi-language/globalization in large .NET project 在C#.NET中实现不固定的多deminsional数组的最佳方法是什么? - What's the best way to implement an unfixed multi-deminsional array in C#.NET? 单租户到多租户 - Single tenant to multi-tenant 在开发多租户asp.net MVC应用程序时要记住什么? - What to keep in mind when developing a multi-tenant asp.net MVC application? 在基于DLL更改的.NET项目中实现缓存清除的最佳方法是什么? - What's the best way to implement cache busting in a .NET project based on DLL changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM