gpt4 book ai didi

ASP.net MVC 4 全局授权过滤器强制登录 AllowAnonymous 操作

转载 作者:行者123 更新时间:2023-12-03 12:55:09 29 4
gpt4 key购买 nike

在我的 .NET MVC 4 中,我添加了一个全局过滤器以保护我的 Controller 。
这是使用以下方法完成的:

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();
}

现在,我想添加一个重置密码页面,就像登录页面一样,匿名用户也应该可以使用它。我创建了我的 Action (在登录 Action 的同一 Controller 下)并用 [AllowAnonymous] 装饰它装饰:
[AllowAnonymous]
public ActionResult ResetPasswordForUser(string message = "")
{
ViewBag.message = message;
return View();
}

然后创建了相应的 View 。

我将此链接添加到我的登录页面:
@Html.ActionLink("Forgot your password?", "ResetPasswordForUser", "Account")

在运行时,当我使用匿名用户单击此链接时,我确实到达了 ResetPasswordForUser 操作,但是当返回 View 时,登录操作被调用,我永远无法真正到达所需的 View 。出于某种原因,即使我使用了 [AllowAnonymous] 装饰,我的请求也会被拦截。

我在这里错过了什么吗?

提前致谢

更新1:

根据 Darin Dimitrov 请求添加我的 ResetPasswordForUser View :
@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>
}

最佳答案

ResetPasswordForUser 使用的 _Layout 文件是否可能? View 正在调用你的 Controller 中的一个 Action (例如在菜单中?),它没有被 AllowAnonymous 修饰。 ?

这将导致这种行为。

关于ASP.net MVC 4 全局授权过滤器强制登录 AllowAnonymous 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15205545/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com