In Web development , some systems require the same user to log on only once at the same time, that is, if a user is logged in, if you log in again before exiting, you need to error.
< Span style= "FONT-SIZE:12PX;" The common approach is to determine if the user is already present in the application when the user logs in, and if there is an error, it is added to application (application is common to all sessions, A unique object for the entire Web application):
Application.add ("Global_user_list", LIST);
Of course, it is also possible to save it using the cache.
< Span style= "FONT-SIZE:12PX;" The next step is to remove the user from application when the user exits, and we can handle it in the Global.asax Session_End event:
void session_end (object sender, EventArgs e)
{
//code to run at the end of the session.
//Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to
//InProc. If the session mode is set to StateServer
//or SQL Server, the event is not raised.
String strUserID = session["Session_user"] as String;
ArrayList list = Application.get ("Global_user_list") as ArrayList;
if (strUserID! = NULL && list! = null)
{
list. Remove (strUserID);
Application.add ("Global_user_list", LIST);
}
}
< Span style= "FONT-SIZE:12PX;" There is no problem, the problem is when the user directly point to browser
There are two ways to handle this:
1, using JavaScript method
Add a piece of JavaScript code to each page:
}
Because the onbeforeunload method will be executed when the browser is closed, refreshed, and page-reversed, it is necessary to determine whether the Close button is clicked or the ALT+F4 is pressed to perform a real shutdown operation.
Then write and session_end the same method in the Logout.aspx Page_Load, adding the event in logout.aspx: onload= "Javascript:window.close ()"
However, there is still a problem, JavaScript may behave differently in different browsers, and it is not judged when it is closed by file.
2, using the XMLHTTP method (this method test down no problem)
Add the following JavaScript to each page (these javascript can also be written in common, each page is introduced)
Myrefresh ();
This article is from the "Rain Wandering Blog" blog, please be sure to keep this source http://kingrain.blog.51cto.com/91779/1600919
How to prevent users from logging on multiple times in ASP.