我阅读了如何在MVC 3中轻松重定向未经身份验证的用户?和 当用户未被授权时重定向到AccessDenied页面,但是一个回答中的链接(即http://wekeroad.com/2008/03/12/aspnet-mvc-securing-your-controller-actions/)失效了。
我放置了
[Authorize(Users = "test")]
public class RestrictedPageController: Controller
{
public ActionResult Index()
{
return View();
}
....
}
而且我在我的web.config文件中已经
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
根据https://dev59.com/GWw15IYBdhLWcg3wMY6L#6770583的建议:
但是当我想要访问/RestrictedPage/Index
时,它必须将我重定向到其他页面(来自其他控制器)。但实际上出现了错误提示:
Server Error in '/Project' Application.
The view 'LogOn' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Account/LogOn.aspx
~/Views/Account/LogOn.ascx
~/Views/Shared/LogOn.aspx
~/Views/Shared/LogOn.ascx
~/Views/Account/LogOn.cshtml
~/Views/Account/LogOn.vbhtml
~/Views/Shared/LogOn.cshtml
~/Views/Shared/LogOn.vbhtml
在登录之前,Logon
页面表单显示正确,但访问 /RestrictedPage/Index
页面时出现上述错误。我可以使用另一个被授权访问 RestrictedPage
页面的用户登录。
我的错误在哪里?如何设置重定向?
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
来重写该方法,否则无法正常工作。 - Snake Eyes[Authorize]
过滤器只是为了更改路径似乎有些过度。 - Lukasfilters.Add(new AuthorizeAttribute());
- Web Developer