简体   繁体   中英

MVC 4 Redirect from login page if authenticated

By using a default MVC 4 web application how can I redirect the user to certain page if he is logged in? What I mean when you try to access a page that is decorated with [Authorize] attribute, it will redirect you to login page and the parameter in the url is the page you tried to access. If login succeeded, you will be redirected to that specific page. Now you are authenticated and you can access that specific page. But if you go back to the login page http://page:port/Account/Login it will still display the login page. How can I redirect the user from login page if he/she is already loged in?

Should I use this in the controller GET Login action from Account Controller?

if(Request.IsAuthenticated)
{
    ...redirect to ...
}

You can use RedirectToAction in your controller like this:

if(Request.IsAuthenticated)
{
    return RedirectToAction("Index","Home");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM