Search code examples
c#asp.net-mvcasp.net-coreasp.net-identityuser-identification

How do I redirect the user to a view once they've logged in and attempted to go back to the login screen ASP NET CORE


I have my auth controller which on successful login redirects the user to my dashboard controller with the user.identity properties populated, so I can use User.IsAuthenticated and User.IsInRole("Admin")

My question is.

Once the user is logged in and are on the dashboard page. How can I redirect them back to the dashboard page if they're already logged in.

Thanks in advance.


Solution

  • So my solution was to simply check if the user was already authenticated in my [HttpGet] login controller as opposed to my [HttpPost] login controller.

    [HttpGet]
    public ActionResult Login()
    {
        if (User.Identity.IsAuthenticated)
            return RedirectToAction("Index", "Dashboard");
    
        return View();
    }
    
    [HttpPost]
    public async Task<IActionResult> Login(LoginViewModel req)
    {
    
        return View(req);
    }