Search code examples
authenticationasp.net-coreasp.net-identityasp.net-core-identity

How to prevent users that are already signed in to access Log in or register page in ASP.NET Core?


How to prevent users that are already signed in to access Log in or register page in ASP.NET Core? Instead, redirect signed in user to index page. Is it possible to do it with a data annotation decorated above the action method?


Solution

  • In the GET request for your login page use this:

    if (User.Identity.IsAuthenticated)
    {
      //Redirect to Home...
    }
    else
    {
      //Return login view...
    }
    

    if the user is logged in, it will just take them back home page. Otherwise, it will take them to login page.