简体   繁体   中英

How to check is session expire or logout in asp.net

How I will identify whether my login session is expired or user logged out.As I am trying to show some message after users session timeout and redirect to login page. I also googled for it but not get satisfactory answer. Can anybody please guide me, how I need to check this.

Thanks in advance

To check your log in session,change the value of session time out in settings of applications. Keep it to some 3-5 minutes so that you can test it quickly. for that time do not perform any action and after some try to access your tool.you must be logged off of you application.

You may set a cookie when login is successful:

Response.Cookies.Add(new HttpCookie("login_status", "1"));

And when user presses "Log out", you either delete the cookie or set it to another value:

Response.Cookies.Add(new HttpCookie("login_status", "0"));

Then it's easy to check in Page_Load event of the login page:

HttpCookie loginStatusCookie = Request.Cookies["login_status"];
if(loginStatusCookie != null && loginStatusCookie.Value == "1")
{
     //User did not log out explicitly.
     //Display timeout message.
}
else
{
     //User either logged out or it is his/her first visit.
     //Display usual greeting.
}

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