I have been trying to make my login page unreachable to already logged in users. They should rather be redirected to their dashboard unless they are not logged in.
I have done the following and neither of them is working. Anyone with a solution please?
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (Request.IsAuthenticated)
{
RedirectToAction("Index", "Dashboard");
}
ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index","Dashboard");
return View();
}
And this too
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
RedirectToAction("Index", "Dashboard");
}
ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index","Dashboard");
return View();
}
Any guide will be appreciated. Thank you
Sorry, I just observed that I was omitting the "return" on the line that ought to redirect the user.
I have added that and it now works
This is the correct code below
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Dashboard");
}
ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index","Dashboard");
return View();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With