私の .NET MVC 4 では、コントローラーを保護するためにグローバル フィルターを追加しています。これは以下を使用して行われます。
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>
My Login アクションは[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 アクションに到達しますが、ビューを返すときに Login アクションが呼び出され、実際に目的のビューに到達することはありません。[AllowAnonymous] 装飾を使用しているにもかかわらず、何らかの理由でリクエストが傍受されます。
ここで何か不足していますか?
前もって感謝します
アップデート1:
私の ResetPasswordForUser ビューを追加する Darin Dimitrov 要求に従って:
@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>
}