Asp.net: unique logon example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.