在我的.NET MVC 4我添加一個全局過濾器,以保護我的控制器。 這是用做:ASP.net MVC 4全球授權過濾器強制登錄的AllowAnonymous行動
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
}
這是從我的Global.cs類調用。
我的web.config文件中包含一個標準配置:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
我的登錄動作,以允許匿名用戶登錄飾[AllowAnonymous]
。
到目前爲止好,一切都很好。這裏是我的登陸行動:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
現在,我想補充這也就像登錄頁面應提供給匿名用戶重置密碼的頁面。我創造了我的行動(的登錄操作同一控制下),並與[AllowAnonymous]
裝修裝飾它:
[AllowAnonymous]
public ActionResult ResetPasswordForUser(string message = "")
{
ViewBag.message = message;
return View();
}
然後創建相應的視圖。
我加入這個鏈接到我的登錄頁面:
@Html.ActionLink("Forgot your password?", "ResetPasswordForUser", "Account")
在運行時,我用我得到的ResetPasswordForUser操作的匿名用戶點擊該鏈接,但返回視圖時登錄動作被調用我永遠無法真正達到理想的視野。出於某種原因,即使我使用[AllowAnonymous]裝飾,我的請求也會被攔截。
我在這裏錯過了什麼嗎?
在此先感謝
UPDATE1:
按照達林季米特洛夫要求加入我的ResetPasswordForUser觀點:
@using TBS.Models
@model TBS.ViewModels.ResetPasswordForUserViewModel
@{
ViewBag.Title = "Reset Password";
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<table class="edit-table rounded bordered" style="width: 400px">
<tr>
<td class="label-td">
@Html.LabelFor(m => m.Username)
</td>
<td>
@Html.TextBoxFor(m => m.Username)
@Html.ValidationMessageFor(model => model.Username)
</td>
</tr>
<tr>
<td class="label-td">
@Html.LabelFor(m => m.Password)
</td>
<td>
@Html.Password("Password")
@Html.ValidationMessageFor(model => model.Password)
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<input type="submit" value="Reset" />
</td>
</tr>
</table>
}
是否有配置一個<授權>部分?如果是這樣,那可能會干擾MVC的授權機制。 – Levi 2013-03-04 16:49:00
我的Web.config文件包含 authentication> –
forhas
2013-03-04 18:59:44
您粘貼部分,而不是<授權>部分。後者會導致問題。此外,請確保您使用System.Web.Mvc.AllowAnonymousAttribute而不是System.Web.Http命名空間中的一個。 –
Levi
2013-03-05 16:28:28