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

Jos*_*osh 3 asp.net asp.net-mvc forms-authentication

我一直试图让我的登录页面无法访问已登录的用户.除非他们没有登录,否则应该将他们重定向到他们的仪表板.

我做了以下工作,但他们都没有工作.有解决方案的人请?

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

        ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index","Dashboard");
        return View();
    }
Run Code Online (Sandbox Code Playgroud)

这也是

  // 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();
    }
Run Code Online (Sandbox Code Playgroud)

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

Jos*_*osh 6

对不起,我刚观察到我在省略了应该重定向用户的行上的"返回".

我已经添加了它,它现在有效

这是下面的正确代码

 // 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();
}
Run Code Online (Sandbox Code Playgroud)