【发布时间】:2012-04-17 10:13:09
【问题描述】:
我是 .NET 和 MVC3 的新手,我目前正在将一个长期存在的经典 ASP 站点转换为 MVC3 应用程序站点。
同一个站点有四个区域,我需要独特的登录页面以及不同级别的访问和安全性。这意味着我不想在我的 web.config 文件中使用默认代码:
<authentication mode="Forms">
<forms loginUrl="~/Account/Logon" timeout="2880" />
</authentication>
相反,我希望能够在我的应用程序中设置区域,然后使用<location> 属性为每个区域设置身份验证和授权规则。比如:
<location path="AreaName">
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/AreaName/Login" timeout="15" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
如果我删除这三行:
<authentication mode="Forms">
<forms loginUrl="~/AreaName/Login" timeout="15" />
</authentication>
然后,当我尝试访问路径“AreaName”中的任何视图时,我首先会被重定向回 ~/Account/LogOn。如果我把这三行放回去,我会得到以下错误:
解析器错误消息:在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的部分是错误的。 此错误可能是由未配置虚拟目录引起的 作为 IIS 中的应用程序。
我已针对此错误进行了搜索,但似乎没有任何建议适用。我已经看到了一个答案,其中逻辑应该放在控制器上而不是 Web 配置中,但这些方法仍然暗示您将为整个站点使用默认的 <authentication>。
我不明白为什么我不能在 <location> 中拥有 <authentication> 属性。对此的任何帮助将不胜感激。
如果我对这个问题的任何措辞有误,我深表歉意。
【问题讨论】:
-
这似乎是因为在 ASP.NET 框架的 Machine.config 文件中,为身份验证部分设置了
allowDefinition="MachineToApplication"的设置。所以我想我的问题变成了,有没有办法覆盖 machine.config 中的设置并在我的应用程序 web.config 文件中将其设置为allowDefinition="Everywhere"?
标签: visual-studio-2010 asp.net-mvc-3 forms-authentication