ASP.NET MVC Beta Authorize 属性使我执行错误的操作

发布于 2024-09-26 09:35:03 字数 652 浏览 8 评论 0 原文

今天我开始使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵强ㄟ 2024-10-03 09:35:03

它不再适用于 RTM

您需要添加

<add key="loginUrl" value="~/Account/LogOn" />

到 Web.Config 中的 appSettings

问题出在 WebMatrix.WebData 中的 ConfigUtil 中

private static string GetLoginUrl()
{

    return ConfigurationManager.AppSettings[FormsAuthenticationSettings.LoginUrlKey] ?? FormsAuthenticationSettings.DefaultLoginUrl;
}



staticFormsAuthenticationSettings()
{
    LoginUrlKey = "loginUrl";
    DefaultLoginUrl = "~/Account/Login";
}

It doesn't work with the RTM any more

You need to add

<add key="loginUrl" value="~/Account/LogOn" />

to the appSettings in the Web.Config

The issues is in ConfigUtil in WebMatrix.WebData

private static string GetLoginUrl()
{

    return ConfigurationManager.AppSettings[FormsAuthenticationSettings.LoginUrlKey] ?? FormsAuthenticationSettings.DefaultLoginUrl;
}



staticFormsAuthenticationSettings()
{
    LoginUrlKey = "loginUrl";
    DefaultLoginUrl = "~/Account/Login";
}
东风软 2024-10-03 09:35:03

ScottGu 在他的博客上回复了类似的问题,这显然是一个错误。

解决方法是将此条目添加

<add key="autoFormsAuthentication" value="false" />

到您的 /> 中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:

<add key="autoFormsAuthentication" value="false" />

to your <appSettings/> section in the web application's root web.config file.

以为你会在 2024-10-03 09:35:03

当我删除bin目录中的WebMatrix*.dll后,一切正常。

After I delete WebMatrix*.dll in bin directory, everything is OK.

左耳近心 2024-10-03 09:35:03

MVC 4 也存在同样的问题。但是,在 MVC 4 上,如果身份验证模式在配置文件中正确设置为“Forms”,如下所示,问题就会消失:

<authentication mode ="Forms">
    <forms loginurl = "your login" timeout ="2880" slidingExpiration="true">
</authentication>

它对我有用。去掉这个模式就会给你带来麻烦。

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:

<authentication mode ="Forms">
    <forms loginurl = "your login" timeout ="2880" slidingExpiration="true">
</authentication>

It works for me. Take out the mode and it gives you trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文