当用户在 mvc3 中未获得授权时重定向到另一个页面 asp.net

本文关键字:另一个 重定向 net asp 授权 mvc3 用户 | 更新日期: 2023-09-27 18:36:30

我读过

如果未在 MVC 3 中进行身份验证,如何轻松重定向?当用户未获得授权但来自答案的链接(表示 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://stackoverflow.com/a/6770583/998696

但是当我想访问/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页面。

我的错误在哪里以及如何设置重定向?

当用户在 mvc3 中未获得授权时重定向到另一个页面 asp.net

默认Authorize属性的行为方式是,当用户未经身份验证未经身份验证但未获得授权时,它将状态代码设置为 401(未授权)。当筛选器将状态代码设置为 401 时,ASP.NET 框架会检查网站是否启用了表单身份验证,然后是否将其重定向到在那里设置loginUrl参数。

如果要更改该行为,例如,如果要将用户重定向到AccessDenied控制器(如果用户已通过身份验证但未获得授权),则必须扩展 Authorize 属性并重写 HandleUnauthorizedRequest 方法。

例如。

public class CustomAuthorize: AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
        else
        {
           filterContext.Result = new RedirectToRouteResult(new 
               RouteValueDictionary(new { controller = "AccessDenied" }));
        }
    }
}

您可以根据需要覆盖HandleUnauthorizedRequest,然后必须标记控制器操作以使用 CustomAuthorize 属性而不是内置属性。

我喜欢马克的答案,
但我不想更改我的所有操作属性
从 [授权] 到 [自定义授权]

我在AccountController
上编辑Login()操作并在显示视图
之前选中Request.IsAuthenticated我认为,如果经过身份验证的用户转到/Account/Logon
我将重定向到/Error/AccessDenied.

    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        if (Request.IsAuthenticated)
        {
            return RedirectToAction("AccessDenied", "Error");
        }
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

因为我不想覆盖AuthorizeAttribute所以我使用了过滤器

public class RedirectFilter : ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!IsAuthorized(filterContext))
        {
            filterContext.Result =
                new RedirectToRouteResult(new RouteValueDictionary(new {controller = "AccessDenied"}));
        }
    }
    private bool IsAuthorized(ActionExecutingContext filterContext)
    {
        var descriptor = filterContext.ActionDescriptor;
        var authorizeAttr = descriptor.GetCustomAttributes(typeof(AuthorizeAttribute), false).FirstOrDefault() as AuthorizeAttribute;
        if (authorizeAttr != null)
        {
            if(!authorizeAttr.Users.Contains(filterContext.HttpContext.User.ToString()))
            return false;
        }
        return true;
    }
}

放置"/account/LogOn"而不是"~/account/LogOn"

是的,正如您在 web.config 中提到的,这是正确的

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

重定向正在查找帐户控制器和登录操作结果。 如果要重定向页面,请在此处更改而不是帐户并登录