简体   繁体   中英

ASP.net display LoginStatus in masterpage by checking Session

I just want to put LogIn Status text in MY ASP master page. When User not logged IN it will show LogIn and If user already logged In it will show LogOut . Once user Clicked logOut ,session will be clear.

<asp:LoginView ID="LoginView1" runat="server" onviewchanged="LoginView1_ViewChanged">
       <AnonymousTemplate>
              <asp:LoginStatus ID="LoginStatus1" runat="server" LoginText="logIn" />
       </AnonymousTemplate>
       <LoggedInTemplate>
              <asp:LoginStatus ID="LoginStatus2" runat="server" LogoutText="loggedout"  />
       </LoggedInTemplate>
</asp:LoginView>  

I am using using System.Web.SessionState; to manage session. I can check user is login or not by using bellow code.

If (Session["logged"] == true) 

Please help me to display LogIn or logOut Status in Master page in Dynamically according to current session status. Thank you in advance.

LoginView control uses the current request's identity to determine whether or not they are logged in, and also the principal's role memberships, if you use the feature where you can show different templates for different roles. So, you won't be able to achieve what you wish using LoginView . Same thing with LoginStatus .

You can roll your own versions of those controls to do what you wish, or attempt to derive your own control from those controls and simply override the part where it figures out if you're logged in or not.

Alternately, you can just place something like this in your master page.

<% if (Session["logged"] is bool && (bool)Session["logged"]) {%>
 Logged in
%> } else { %>
 Logged out
<% } %>

However, I'd recommend you just rely on the request's identity to determine whether or not a user is logged in. A) it's built-in, fully supported, and you don't have to do anything extra B) Sessions are not durable. You can access the request's user in a page, control, etc. by accessing the User.Identity.IsAuthenticated property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM