防止登录用户访问 asp.net mvc 5 中的登录页面

Prevent logged in users from accessing login page in asp.net mvc 5

我一直在尝试让已经登录的用户无法访问我的登录页面。他们应该被重定向到他们的仪表板,除非他们没有登录。

我做了以下操作,但都没有用。请问有人有解决办法吗?

    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        if (Request.IsAuthenticated)
        {
            RedirectToAction("Index", "Dashboard");
        }

        ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index","Dashboard");
        return View();
    }

还有这个

  // 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();
    }

任何指南将不胜感激。谢谢

抱歉,我刚刚注意到我在应该重定向用户的行中省略了 "return"。

我已经添加了,现在可以使用了

下面是正确的代码

 // 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();
}