我第一次使用 MVC 4 来检查与 MVC 3 相比有哪些更改/添加/等。
首先,我创建了一个空白的 MVC 4 Web 应用程序并从头开始构建。
我注意到与 MVC 4 不同的第一件事是以下 web.config 设置对网页的可访问性没有影响:
<configuration>
<location path="">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
.....
</configuration>
回到 MVC 3,上面的授权设置将拒绝所有匿名用户访问站点内的任何内容。但是,如果我将相同的设置添加到 MVC4 Web.config 文件中,匿名者可以自由支配他/她选择的 URL。
我需要在 MVC 4 中做什么才能像在 MVC 3 中那样锁定所有路径?
最佳答案
看看Securing your ASP.NET MVC 4 App and the new AllowAnonymous Attribute .
You cannot use routing or web.config files to secure your MVC application (Any Version). The only supported way to secure your MVC application is to apply the Authorize attribute...
Quote
MVC uses routes and does not map URLs to physical file locations like WebForms, PHP and traditional web servers. Therefore using web.config will definitely open a security hole in your site.
The product team will have a communication if this changes in the future, but for now it is without exception the rule.
例子:
从默认的 ASP.Net MVC 项目(互联网/内联网)开始。
编辑 web.config 添加:
<location path="Home">
<system.web>
<authorization>
<deny users="*">
</authorization>
</system.web>
</location>
运行项目,默认使用默认路由
/Home/Index
并且您会看到内容,只需绕过 web.config 而不更改默认模板。为什么?因为 ASP.Net 管道正在将请求的 URL 与 web.config 中指定的位置进行比较。然而,后 授权事件已在路由发生的管道中执行(默认路由或自定义路由),并允许访问所谓的受限区域。此外,任何 MVC
Redirect()
还将绕过与在授权管道事件之后再次进行路由相同的安全措施。我认为任何人都不应该接受某种工作安全。第一次就做正确的事,不要偷懒,使用一些不是设计用于特定技术的东西。
关于asp.net-mvc - 如何锁定 ASP.NET MVC 中的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11765030/