Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...
}
like image 547
user2818430 Avatar asked Jan 12 '14 18:01

user2818430


People also ask

How do I redirect after successful login?

Simply enter a login URL and logout URL into the 'All Other Users' section. Then, click the 'Save Changes' button. When a new user signs up on your website, WordPress redirects them to the login page. You can set up a redirect URL to send them to any other page on your website.

What is the difference between redirect and RedirectToAction in MVC?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.


1 Answers

You can use RedirectToAction in your controller like this:

if(Request.IsAuthenticated)
{
    return RedirectToAction("Index","Home");
}
like image 100
Selman Genç Avatar answered Jan 04 '23 19:01

Selman Genç