Using httpmoduler to implement a web program at the same time, only one user instance logs in

xiaoxiao2021-04-01  204

/ * Author: bull (cow Queensland light) QQ: 273352165 MSN: niukl@hotmail.com statement: free to use, please keep this information if you have any change, remember to tell me * /!

When I use the ASP.NET to develop a web site, sometimes it does not let the same user name to log in multiple times at the same time.

In order not to affect the original whole site, I chose to use HttpModuler to implement.

Let all Page first inherited from our own Page: BasePage class and implement IsigLogin interface. The relevant code is as follows:

public

Interface

IsingLogin

{String sigleuserloginid {get;} void sigleuserlogout ();

public

Class

Basepage: System.Web.ui.page, BNET.WEB.MODULERS.ISINGLOGIN

{Public BasePage () {// // Todo: Add Constructor Logic //} Protected Override Void OnLoad (Eventargs E) {Base.onLoad (E); IF (session ["UserID"] == NULL) {Response.write ("You haven't logged in"); Response.Redirect ("login.aspx");}}}}}}}}}} ISINGINEN Member #Region ISINGLOGIN Member Public String SigleUserLoginid {Get {IF (Session ["UserID"]! = Null) {RETURN session ["userid"]. TOSTRING ();} else Return "";}} public void sigleuserlogout () {session.abandon (); response.write ("You have already logged over elsewaters! ");} #Endregion}

Then add httpmoduler in Web.config:

<

SYSTEM

.web

>

<

Httpmodules

>

<

Add

Name

= "Singlelogin"

Type

= "BNET.WEB.MODULERS.SINGLOGINMODULER"

/>

Httpmodules

>

System.Web

>

The relevant SIGLOGINMODULER 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

{/ ** ////

/// SingleLoginModuler summary description /// public class SingleLoginModuler: System.Web.IHttpModule {const string sigle_login_userid = "evlon_siglelogin_userid"; const string sigle_pre_logout_sessionid = "evlon_sigle_pre_logout_sessionid" ; static StringLifeValueDictionary loginedUserIdDictionary = null; static StringLifeValueDictionary LoginedUserIdDictionary {get {if (loginedUserIdDictionary == null) {loginedUserIdDictionary = new StringLifeValueDictionary ();} else {List listRemove = new List (); StringLifeValueDictionary.Enumerator iter = LoginedUseridDictionary.GeteNumerator (); while (ore.movenext ()) {if (iter.current.value.life

new StringLifeValueDictionary ();} else {List listRemove = new List (); StringLifeValueDictionary.Enumerator iter = preLogoutSessionIdDictionary.GetEnumerator (); while (iter.MoveNext ()) {if (iter.Current.Value. life

string.Empty) {if (PreLogoutSessionIdDictionary.ContainsKey (context.Session.SessionID)) {// This user should forcibly logged PreLogoutSessionIdDictionary.Remove (context.Session.SessionID); Page page = (Page) httpHandler; page.PreInit = new EventHandler (page_PreInit);} else if {LoginedUserIdDictionary.Add (suid, new LifeValue (context.Session.SessionID)) (LoginedUserIdDictionary.ContainsKey (suid)!);}}}} void page_PreInit (object sender, EventArgs e) {Page Page; IsingLogin SL = Page AsingLogin; IF (SL! = Null) {sl.sigleUserLogout (); Page.Response.end ();}} void contexT_postrequestHandleRexecute (O bject sender, EventArgs e) {// find the current user and from the same user ID in the LogineduserId SessionId HttpApplication context = sender as HttpApplication; IHttpHandler httpHandler = context.Context.CurrentHandler; if (httpHandler is ISingleLogin) {ISingleLogin sl = httpHandler as ISingleLogin; string suid = sl.SigleUserLoginId; if (suid = string.Empty!) {if (LoginedUserIdDictionary.ContainsKey (suid)) {string sessionId = LoginedUserIdDictionary [suid] .value; if (sessionId =!

转载请注明原文地址:https://www.9cbs.com/read-131223.html

New Post(0)