开发者

Logging out a user from the site

开发者 https://www.devze.com 2023-02-03 15:21 出处:网络
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 log

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:

  1. The user clicks on the autogenerated Logout Button of the LoginStatus control
  2. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消