How to hide the main page menu until the user logs into asp.net

I am creating a home page with a navigation bar. I made the index page as the login page, so I use the login control in index.aspx, which is registered on the main page.

Now my question is: how can I hide the navigation bar that is on the main page until the user logs in, after the user successfully logs in, the navigation bar appears

+6
source share
4 answers

Use this on the main page (C # code)

<% if (HttpContext.Current.User.Identity.IsAuthenticated ) { %> <div>navigation html when is authenticated</div> <% } else { %> <div>navigation html when is NOT authenticated</div> <% } %> 
+7
source

In web forms, you can use the LoginView control to display different content depending on the user's authentication status:

 <asp:LoginView ID="LoginView1" Runat="server"> <LoggedInTemplate> <div>Navigation Bar</div> </LoggedInTemplate> <AnonymousTemplate> <div>Unauthenticated content</div> </AnonymousTemplate> </asp:LoginView> 
+3
source
  protected void Page_Load(object sender, EventArgs e) { String path = HttpContext.Current.Request.Url.AbsolutePath; if (path == "/login.aspx") { Menu1.Visible = false; } } 
+3
source

If you are using an Asp.net menu control, put the following code in the page load:

 protected void Page_Load(object sender, EventArgs e) { Menu1.Visible = User.Identity.IsAuthenticated; } 

Otherwise, place the navigation bar in the placeholder and show / hide the placeholder.

+1
source

Source: https://habr.com/ru/post/907696/


All Articles