JS Code:
<SCRIPT type = "text/JavaScript">
String. Prototype. Trim = function (){
Return this. Replace (/(^ \ s +) | \ s + $/g ,"");
}
VaR x = 0;
Function myrefresh (){
VaR httprequest = new activexobject ("Microsoft. XMLHTTP ");
Httprequest. Open ("get", "sessionout. aspx", false );
Httprequest. Send (null );
X ++;
If (x <2) // 60 times, that is, the actual session expiration time is 30 minutes.
{
SetTimeout ("myrefresh ()", 30*1000); // 30 seconds
}
}
Myrefresh ();
Window. onbeforeunload = function () // Author: meizz
{
VaR n = Window. event. screenx-window. screenleft;
VaR B = n> document.doc umentelement. scrollwidth-20;
If (B & window. event. clienty <0 | window. event. altkey ){
$. Ajax (
{
Type: "Post ",
URL: "sessionhandler. ashx ",
Success: function (){
}
})
}
}
</SCRIPT>
Webconfig sessionstate settings:
<Sessionstate mode = "inproc" stateconnectionstring = "TCPIP = 127.0.0.1: 42424" sqlconnectionstring = "Data Source = 127.0.0.1; trusted_connection = yes" cookieless = "false" timeout = "1"/>
Determine whether a user is logged on:
Public static bool userislogin (string userid)
{
Bool islogin = false;
Arraylist list = httpcontext. Current. application. Get ("global_user_list") as arraylist;
If (list = NULL)
{
List = new arraylist ();
}
If (list. Contains (userid ))
{
Islogin = true;
}
Else
{
List. Add (userid );
Httpcontext. Current. application. Add ("global_user_list", list );
}
Return islogin;
}
When the session expires or the user exits, the user is removed from the arraylist:
Void session_end (Object sender, eventargs E)
{
// String url = httpcontext. Current. Request. url. absolutepath. tostring ();
// If. indexof ("SysAdmin") =-1)
//{
String loginlogid = convert. tostring (session ["loginlogid"]);
If (! String. isnullorempty (loginlogid ))
{
Erp_crm.bll.crm_loginlog bllloginlog = new erp_crm.bll.crm_loginlog ();
Erp_crm.model.crm_loginlog loginlog = bllloginlog. GetModel (loginlogid );
Loginlog. exittime = datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS ");
Bllloginlog. Update (loginlog );
}
String struserid = convert. tostring (session ["userid"]);
Arraylist list = application. Get ("global_user_list") as arraylist;
If (struserid! = NULL & list! = NULL)
{
List. Remove (struserid );
Application. Add ("global_user_list", list );
}
// Response. Redirect ("login. aspx ");
//}
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session Mode
// If it is set to StateServer or sqlserver, this event is not triggered.
}
Secure exit: Session. Abandon ();
Reference: http://www.cnblogs.com/myaspnet/archive/2010/11/03/1867703.html