Redirect to another page when user is not authorized in asp.net mvc3

前端 未结 5 1877
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 07:20

I\'ve read

How to easily redirect if not authenticated in MVC 3? and Redirect to AccessDenied page when user is not authorized but the link from an answer (means h

5条回答
  •  臣服心动
    2020-12-05 08:14

    I like Mark's Answer,
    but I don't want to change all of my action attributes
    from [Authorize] to [CustomAuthorize]

    I edit Login() action on AccountController
    and check Request.IsAuthenticated before show view
    I think, if the authenticated user go to /Account/Logon,
    I will redirect to /Error/AccessDenied.

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

提交回复
热议问题