Asp.net-Mvc

ASP.NET MVC Beta Authorize 屬性將我發送到錯誤的操作

  • August 12, 2013

今天我開始玩 MVC 3 Beta。從預設 MVC 3 模板中的應用程序開始,在 Home 控制器中添加了一個新操作,如下所示(帶有視圖)

[Authorize]
public ActionResult Secured()
{
   ViewModel.Message = "This is secured area, only authenticated users should be here.";
   return View();
}

現在,當我嘗試導航到 Secured 操作時,我收到 404 page not found 錯誤。

這是我的 web.config 中的身份驗證部分。

<authentication mode="Forms">
 <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

如果我理解正確,Authorize 屬性應該會導致 401 未經授權的 HTTP 響應,該響應應該被身份驗證處理程序攔截並將我重定向到 loginUrl。這應該導致帳戶/登錄操作。

我的 MVC 2 應用程序按預期工作,並將我帶到 Account/LogOn 操作,我錯過了什麼嗎?還是這是 MVC 3 beta 中的錯誤?

ScottGu 在他的部落格上回答了一個類似的問題,這顯然是一個錯誤。

解決方法是添加此條目:

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

<appSettingsWeb 應用程序的根 web.config 文件中的 /> 部分。

它不再適用於 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";
}

引用自:https://stackoverflow.com/questions/3877339