Tuesday, July 9, 2013

Avoid showing Login page if the user is already logged in - ASP.NET MVC


To avoid showing the login page again if the user is already logged in successfully, you can use the below code in your controller action for the login view.

       [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            if (Request.IsAuthenticated)//check the user is logged in
            {
                return RedirectToAction("Index", "Home", null);// redirect to home page if authenticated
            }
            else
            return View(); // else show the login page
        }

No comments:
Write comments
Recommended Posts × +