今天我开始使用 MVC 3 Beta。从默认 MVC 3 模板中的应用程序开始,在 Home 控制器中添加了一个新操作,如下所示(带有其视图)
[Authorize]
public ActionResult Secured()
{
ViewModel.Message = "This is secured area, only authenticated users should be here.";
return View();
}
现在,当我尝试导航到安全操作时,出现 404 页面未找到错误。
这是我的 web.config 中的身份验证部分。
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
如果我理解正确的话,Authorize 属性应该会导致 401 未经授权的 HTTP 响应,该响应应该被身份验证处理程序拦截并将我重定向到 loginUrl。这应该会导致帐户/登录操作。
我的 MVC 2 应用程序按预期工作并带我进行帐户/登录操作,我是否遗漏了什么?或者这是 MVC 3 beta 中的错误吗?
Today I started playing with the MVC 3 Beta. Started with an application from default MVC 3 template, added a new action in the Home controller as follows(with a view for it)
[Authorize]
public ActionResult Secured()
{
ViewModel.Message = "This is secured area, only authenticated users should be here.";
return View();
}
Now when I try to go to navigate to Secured action I get a 404 page not found error.
Here is the authentication section from my web.config.
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
If I understood it right the Authorize attribute should result in a 401 unauthorized HTTP response which should be intercepted by the authentication handler and redirect me to the loginUrl. Which should result in Account/LogOn action.
My MVC 2 application works as expected and takes me to Account/LogOn action, am I missing something? or Is this a bug in MVC 3 beta?
发布评论
评论(4)
它不再适用于 RTM
您需要添加
到 Web.Config 中的 appSettings
问题出在 WebMatrix.WebData 中的 ConfigUtil 中
It doesn't work with the RTM any more
You need to add
to the appSettings in the Web.Config
The issues is in ConfigUtil in WebMatrix.WebData
ScottGu 在他的博客上回复了类似的问题,这显然是一个错误。
解决方法是将此条目添加
到您的/> 中Web 应用程序的根 web.config 文件中的部分。
ScottGu replies to a similar question on his blog that this is apparently a bug.
The workaround is to add this entry:
to your
<appSettings
/> section in the web application's root web.config file.当我删除bin目录中的WebMatrix*.dll后,一切正常。
After I delete WebMatrix*.dll in bin directory, everything is OK.
MVC 4 也存在同样的问题。但是,在 MVC 4 上,如果身份验证模式在配置文件中正确设置为“Forms”,如下所示,问题就会消失:
它对我有用。去掉这个模式就会给你带来麻烦。
MVC 4 exhibits the same problem. However on MVC 4 if authentication mode is correctly set to ="Forms" in the configuration file, like in the following, the problem disappears:
It works for me. Take out the mode and it gives you trouble.