During web development, some systems require the same user to log on only once at the same time, that is, if a user has logged on, an error must be reported if the user logs on again before exiting.
A common solution is to determine whether the user already exists in the Application when logging on to the user. If the user exists, an error is reported. If the user does not exist, it is added to the Application (the Application is shared by all sessions, the unique object of the entire web application ):
The following is a reference clip:
| String strUserId = txtUser. Text; ArrayList list = Application. Get ("GLOBAL_USER_LIST") as ArrayList; If (list = null) { List = new ArrayList (); } For (int I = 0; I <list. Count; I ++) { If (strUserId = (list [I] as string )) { // The error message is displayed when you have logged on. LblError. Text = "this user has logged on "; Return; } } List. Add (strUserId ); Application. Add ("GLOBAL_USER_LIST", list ); |
Of course, you can use Cache and other storage methods here.
The next step is to remove the user from the Application when the user exits. We can handle it in the Session_End event of Global. asax:
The following is a reference clip:
- Three pages in total:
- Previous Page
- 1
- 2
- 3
- Next Page