How to hide a sidebar on a login page using server events

Mb 0 Reputation points
2025-05-08T18:16:57.7+00:00

I have a website made with ASP.NET Maker that has a login page that includes a sidebar, which should be hidden when the user is not logged in. What is the best way to implement this functionality using server events?

ASP.NET Core Training
ASP.NET Core Training
ASP.NET Core: A set of technologies in the .NET Framework for building web applications and XML web services.Training: Instruction to develop new skills.
66 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 75,561 Reputation points Moderator
    2025-05-08T18:26:13.71+00:00

    the login page could use a different layout, or the layout could check for authentication before rendering the sidebar.

    0 comments No comments

  2. SurferOnWww 4,396 Reputation points
    2025-05-08T21:40:36.6766667+00:00

    I have a website that has a login page that includes a sidebar, which should be hidden when the user is not logged in. What is the best way to implement this functionality using server events?

    See the code of Views/Shared/_LoginPartial.cshtml. It will provide you with a clue to hide a sidebar from anonymous users.

    The code uses SignInManager.IsSignedIn(User) to judge if the user is logged in as follows:

    @using Microsoft.AspNetCore.Identity
    @inject SignInManager<IdentityUser> SignInManager
    @inject UserManager<IdentityUser> UserManager
    
    <ul class="navbar-nav">
    @if (SignInManager.IsSignedIn(User))
    {
        <li class="nav-item">
            <a  class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity?.Name!</a>
        </li>
        <li class="nav-item">
            <form  class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
                <button  type="submit" class="nav-link btn btn-link text-dark">Logout</button>
            </form>
        </li>
    }
    else
    {
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
        </li>
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
        </li>
    }
    </ul>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.