the login page could use a different layout, or the layout could check for authentication before rendering the sidebar.
How to hide a sidebar on a login page using server events
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
2 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 75,561 Reputation points Moderator
2025-05-08T18:26:13.71+00:00 -
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>