Click here to Skip to main content
14,922,433 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
hi to all
How to end user session when browser closed or web page is logout ?
In my website i use session form login to logout(i am using username and password) ,if i click logout it redirect to login page,if i click back button in the browser it go previous page,here i want to abandon session and if click the back button it dont go to previous page it stay in login page

pls reply asap
thanx in advance
Posted
Comments
a1mimo 1-Nov-12 8:28am
   
What kind of authentication you use in your website?
Aravindbpanchu 1-Nov-12 22:37pm
   
authentication mode="Forms"
/authentication

Please refer to this article
How to end user session when browser closed[^]
   
Add Global.asax page:

add the following code in that file.
in the Application_End method clear all your session data

C#
<![CDATA[<%@ Application Language="C#" %>]]>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
        //  Clear Session Data here
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
     
    }

    void Session_End(object sender, EventArgs e) 
    {
       

    }
       
</script>


i hopes it hepls u.
   
Comments
Aravindbpanchu 4-Nov-12 21:35pm
   
ok but i dont want to go previous page(i.e if i click logout button it redirect to login page if i click back button in browser it goes to previous page)

for example u take bank website if u login and u see the ur transaction details,balance ...etc after that u click logout button it redirect to login page in that if u click back button in browser it say your session expire pls login again or it stay in login page.
this is i want. i clear session in logout page
pls reply asap thanx in advance
Sanjeev Alamuri 4-Nov-12 23:55pm
   
okay, after login page, you are navigating to another page. so in another page take a one session variable. when you press back button it will navigates to login page, so there check this session variable, if it is not empty or null, then display the expire content.
Login page:

C#
if(!isPostBack)
{
  if(Session["AfterLogin"]!=null && Session["AfterLogin"]=="AfterLogin")
  {
      Response.Redirect("~/SessionExpire.aspx");
  }
  else
  {
     //Your logic for login page.
  }
}


in Another page: (afterlogin page)

C#
if(!isPostBack)
{
  Session["AfterLogin"]="AfterLogin";
  // your logic goes here.
}


i hope it helps you.
   
Comments
Aravindbpanchu 8-Nov-12 22:43pm
   
thanx for ur reply but how this work ?
i am not understand.i want do not go to previous page after logout.in my webpage if i click logout button it open logout page in page load i write code for clear all sessions,remove session and redirect to login page.after that if click back button in browser it goes to previous page .
Sanjeev Alamuri 9-Nov-12 1:24am
   
Then disable back button in browser.

in master page or ASPX page add onload event to body tag.
<body önload="history.go(+1);">

it will not redirect to back page.
The following code will end session, sign out and redirect to login page as well as adding the page the user was on to the url as "?ReturnUrl=*.aspx"
VB
'Do your "Is user logged in" check code here
' If not authenticated:
FormsAuthentication.SignOut()
FormsAuthentication.RedirectToLoginPage()
Reponse.End() '<-- IMPORTANT: the RedirectToLoginPage executes on server side but rest of code/logic also completes, so Response.End is required for FormsAuthentication.SignOut to work (a workaround).
   
v2
Two Options, both already mentioned:

1) this.Session.Abandon(); or Session.Abandon();

2) System.Web.Security.FormsAuthentication.SignOut();
   
Comments
[no name] 11-Sep-13 9:50am
   
If both options are "already mentioned" what possible value does your "solution" add to this year old already answered question?
ketan italiya 12-Sep-13 0:51am
   
this question is not solved so i guess it need reply..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900