When we use ASP. NET to develop Web websites, sometimes we do not allow the same user name to log on multiple times at the same time.
In order not to affect the entire website, I chose to use httpmod.pdf.
Let all pages inherit from their own page class: basepage class and implement the isiglelogin interface. RelatedCodeAs follows:
Public InterfaceIsinglelogin {StringSigleuserloginid {Get ;}VoidSigleuserlogout ();}
Basepage class:
Public Class Basepage: system. Web. UI. Page, bnet. Web. modulers. isinglelogin { Public Basepage (){ // // Todo: add the constructor logic here // } Protected Override Void Onload (eventargs e ){ Base . Onload (E ); If (Session [ "Userid" ] = Null ) {Response. Write ( "You have not logged on" ); Response. Redirect ( "Login. aspx" ) ;}# Region isinglelogin Member Public String Sigleuserloginid {get { If (Session [ "Userid" ]! = Null ){ Return Session [ "Userid" ]. Tostring ();} Else Return "" ;}} Public Void Sigleuserlogout () {session. Abandon (); response. Write ( "You have logged on elsewhere. Force the login to exit this logon! " );} # Endregion }
Then add httpmod.pdf to Web. config:
<System. Web> "Singlelogin"Type ="Bnet. Web. modulers. singleloginmoders"/> </Httpmodules> </system. Web>
The related sigleloginmod.pdf code is as follows:
Using System; Using System. Collections. Generic; Using System. Data; Using System. configuration; Using System. Web; Using System. Web. UI; Namespace Bnet. Web. modulers { //// <Summary> /// Summary of singleloginmod.pdf /// </Summary> Public Class Singleloginmodmodules: system. Web. ihttpmodule { Const String Sigle_login_userid = "Evlon_siglelogin_userid" ; Const String Sigle_pre_logout_sessionid ="Evlon_sigle_pre_logout_sessionid" ; Public Static Stringlifevaluedictionary usablegetter ( Ref Stringlifevaluedictionary DIC ){ If (DIC = Null ) {DIC = New Stringlifevaluedictionary ();} Else {List < String > Listremove = New List < String > (); Stringlifevaluedictionary. enumerator iter = DIC. getenumerator (); While (ITER. movenext ()){ If (ITER. Current. value. Life <datetime. Now) {listremove. Add (ITER. Current. Key );}} Foreach ( String Key In Listremove) {DIC. Remove (key );}} Return Dic ;} Static Stringlifevaluedictionary logineduseriddictionary = Null ; Static Stringlifevaluedictionary logineduseriddictionary {get { Return Usablegetter ( Ref Logineduseriddictionary );}} Static Stringlifevaluedictionary prelogoutsessioniddictionary = Null ; Static Stringlifevaluedictionary prelogoutsessioniddictionary {get { Return Usablegetter ( Ref Prelogoutsessioniddictionary );}} Public Singleloginmod.pdf (){ // // Todo: add the constructor logic here // } # Region ihttpmodule Member Public Void Dispose (){} Public Void Init (httpapplication context) {context. prerequesthandlerexecute + = New Eventhandler (context_prerequesthandlerexecute); context. postrequesthandlerexecute + = New Eventhandler (context_postrequesthandlerexecute );} Void Context_prerequesthandlerexecute (Object Sender, eventargs e) {httpapplication context = sender As Httpapplication; ihttphandler httphandler = context. Context. currenthandler; isinglelogin SL = httphandler As Isinglelogin; If (SL! = Null ){ String SUID = SL. sigleuserloginid; If (SUID! = String . Empty ){ If (Prelogoutsessioniddictionary. containskey (context. session. sessionid )){ // This user should be forced to log out Prelogoutsessioniddictionary. Remove (context. session. sessionid); page = (PAGE) httphandler; page. preinit + = New Eventhandler (page_preinit );} Else If (! Logineduseriddictionary. containskey (SUID) {logineduseriddictionary. Add (SUID, New Lifevalue (context. session. sessionid ));}}}} Void Page_preinit (Object Sender, eventargs e) {page = sender As Page; isinglelogin SL = page As Isinglelogin; If (SL! = Null ) {SL. sigleuserlogout (); page. response. End ();}} Void Context_postrequesthandlerexecute ( Object Sender, eventargs e ){ // Find the sessionid of the same user ID as the current user from logineduserid Httpapplication context = sender As Httpapplication; ihttphandler httphandler = context. Context. currenthandler; isinglelogin SL = httphandler As Isinglelogin; If (SL! = Null ){ String SUID = SL. sigleuserloginid; If (SUID! = String . Empty ){ If (Logineduseriddictionary. containskey (SUID )){ String Sessionid = logineduseriddictionary [SUID]. Value ;If (Sessionid! = Context. session. sessionid ){ If (! Prelogoutsessioniddictionary. containskey (sessionid) {prelogoutsessioniddictionary. Add (sessionid, New Lifevalue (SUID);} logineduseriddictionary. Remove (SUID );}} Else {Logineduseriddictionary. Add (SL. sigleuserloginid, New Lifevalue (context. session. sessionid ));}}}} # Endregion }Public Class Lifevalue { Public String Value ; Public Datetime life; Public Lifevalue ( String Value ){ This . Value = Value ; This . Life = datetime. Now. addminutes (httpcontext. Current. session. Timeout + 5 );}} Public Class Stringlifevaluedictionary: dictionary < String , Lifevalue> {} Public Interface Isinglelogin { String Sigleuserloginid {Get ;} Void Sigleuserlogout ();}}
In this way, you can implement the function only by modifying the relevant code (only two or three lines) in your own basepage.
Complete code
Original address: http://evlon.cnblogs.com QQ: evlion@qq.com