简体   繁体   English

使用子域保护MVC应用程序

[英]securing an mvc application using subdomains

I have an mvc application that I allow businesses to log in using https://storea.mydomain.com https://storeb.mydomain.com etc etc 我有一个mvc应用程序,允许企业使用https://storea.mydomain.com https://storeb.mydomain.com等登录

Each of the businesses has users created and I have a table that matches business ID to EmployeeId. 每个业务都有创建的用户,我有一个将业务ID与EmployeeId匹配的表。 I am trying to lock the application down so an authenticated user of businessA cant access information from BusinessB. 我试图锁定该应用程序,以使经过认证的businessA用户无法从BusinessB访问信息。

Where is the best place for me to be checking the user is allowed to access the subdomain? 我要检查允许用户访问子域的最佳位置是哪里? Do I override the OnActionExecuted action checking what the subdomain is then look at a session value to see if they match, if different log them out. 我是否重写OnActionExecuted操作,检查子域是什么,然后查看会话值以查看它们是否匹配,如果不同则将其注销。

Or this there a more elegant way to do this? 还是有一种更优雅的方式来做到这一点?

Suggestions and advise on best practices would be great! 有关最佳做法的建议和建议会很棒! thank you 谢谢

A custom Authorize attribute seems a good place to perform this. 定制的Authorize属性似乎是执行此操作的好地方。 You could override the AuthorizeCore method where you will have access to the HttpContext and you will be able to perform the custom authorization logic based on the subdomain. 您可以覆盖AuthorizeCore方法,在该方法中您可以访问HttpContext,并且可以基于子域执行自定义授权逻辑。

As I understand it, your MVC application uses a single database to store the information for all the businesses you have. 据我了解,您的MVC应用程序使用单个数据库来存储您拥有的所有业务的信息。 This is why you'd be worried about Bussines A accesing Bussines B information. 这就是为什么您会担心Bussines A获取Bussines B信息的原因。

So for me, your database is the place to check which information can access each user depending on the business they belong to. 因此,对我来说,您可以在数据库中检查哪些信息可以访问每个用户,具体取决于他们所属的企业。

I think you don't even need to use subdomains for this. 我认为您甚至不需要为此使用子域。

In cases like these I use the repository pattern and in every query/data access you ensure you pass in a where clause that contains the business id. 在这种情况下,我将使用存储库模式,并且在每次查询/数据访问中,请确保您传递包含业务ID的where子句。 ex: 例如:


select * from orders where orderid=@orderId and companyID=@companyId

It is very important to do this on the query as you want data rules in place to prevent improper querying. 在查询上执行此操作非常重要,因为您需要适当的数据规则以防止查询不正确。 Doing this on an Authorize attribute for example doesn't guarantee someone hasn't tampered with data to load another company's information. 例如,对Authorize属性执行此操作并不能保证某人没有篡改数据以加载另一家公司的信息。

In addition I have the http://mvcsecurity.codeplex.com/ extensions to help secure ids stored on pages to help prevent tampering. 此外,我还有http://mvcsecurity.codeplex.com/扩展名,可帮助保护存储在页面上的ID,以防止篡改。

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

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