I was adding login and logout functionality to my ASP.NET website. While I am able to make the user log in by checking the username and password but on some pages should be available only if he is logged in. I am doing this by storing the user's value in a session
Secondly, I am using a Lin开发者_开发知识库k button
which changes to Logged in as example
. So, how does the user log out?
You kill the session:
Session.Abandon();
You can have 2 server side LinkButtons on your page and have their visibility be mutually exclusive.
LoginLinkButton.Visible = ....;
LogoutLinkButton.Visible = !LoginLinkButton.Visible
You might spend a look at ASP.NET Login Controls Overview
Two ways to logout a user are:
- The user clicks on the autogenerated Logout Button of the LoginStatus control
- The manually abandon the session in the codebehind
Use this snippet:
Session.Abandon();
System.Web.Security.FormsAuthentication.RedirectToLoginPage();
About this whole visibility thing based upon the current login state, you can use LoginView Control, which is exactly what you are looking for, instead of manually assigning the Visibility property as Babak Naffas suggested
精彩评论