当前位置:首页 > asp.net网站实现单点登陆的简单方法

asp.net网站实现单点登陆的简单方法

点击次数:2629  更新日期:2013-06-07

我在们使用ASP.Net开发WEB网站时,有的时候是不让同一个用户名在同一时间进行多次登陆的。
为了不影响原来的整个网站,我选择使用了HttpModuler来实现。
先让所有的Page从自己的Page类:BasePage类继承,并实现 ISigleLogin接口。相关代码如下:

publicinterfaceISingleLogin\r\n{\r\n    stringSigleUserLoginId{get;}\r\n    voidSigleUserLogout();\r\n}\r\npublicclassBasePage:System.Web.UI.Page,BNet.Web.Modulers.ISingleLogin\r\n{\r\n    publicBasePage()\r\n    {\r\n    //\r\n    //TODO:在此处添加构造函数逻辑\r\n    //\r\n    }\r\n    protectedoverridevoidOnLoad(EventArgse)\r\n    {\r\n        base.OnLoad(e);\r\n        if(Session[UserId]==null)\r\n        {\r\n        Response.Write(你还没有登陆);\r\n        Response.Redirect(login.aspx);\r\n        }\r\n    }\r\n    ISingleLogin成员#regionISingleLogin成员\r\n    publicstringSigleUserLoginId\r\n    {\r\n        get\r\n        {\r\n            if(Session[UserId]!=null)\r\n            {\r\n            returnSession[UserId].ToString();\r\n            }\r\n            else\r\n            return;\r\n        }\r\n    }\r\n    publicvoidSigleUserLogout()\r\n    {\r\n        Session.Abandon();\r\n        Response.Write(你在别处已经登陆,强制退出本次登陆!);\r\n    }\r\n}\r\n然后在Web.config中加入HttpModuler:\r\nsystem.web\r\nhttpModules\r\naddname=SingleLogintype=BNet.Web.Modulers.SingleLoginModuler/\r\n/httpModules\r\n/system.web\r\n相关的SigleLoginModuler代码如下:[依评论修改后]\r\nusingSystem;\r\nusingSystem.Collections.Generic;\r\nusingSystem.Data;\r\nusingSystem.Configuration;\r\nusingSystem.Web;\r\nusingSystem.Web.UI;\r\nnamespaceBNet.Web.Modulers\r\n{\r\n    /**////summary\r\n    ///SingleLoginModuler的摘要说明\r\n    ////summary\r\n    publicclassSingleLoginModuler:System.Web.IHttpModule\r\n    {\r\n    conststringsigle_login_userid=evlon_siglelogin_userid;\r\n    conststringsigle_pre_logout_sessionid=evlon_sigle_pre_logout_sessionid;\r\n    publicstaticStringLifeValueDictionaryUsableGetter(refStringLifeValueDictionarydic)\r\n    {\r\n        if(dic==null)\r\n        {\r\n            dic=newStringLifeValueDictionary();\r\n        }\r\n        else\r\n        {\r\n            ListstringlistRemove=newListstring();\r\n            StringLifeValueDictionary.Enumeratoriter=dic.GetEnumerator();\r\n            while(iter.MoveNext())\r\n            {\r\n                if(iter.Current.Value.lifeDateTime.Now)\r\n                {\r\n                    listRemove.Add(iter.Current.Key);\r\n                }\r\n            }\r\n            foreach(stringkeyinlistRemove)\r\n            {\r\n                dic.Remove(key);\r\n            }\r\n        }\r\n        returndic;\r\n    }\r\n    staticStringLifeValueDictionaryloginedUserIdDictionary=null;\r\n    staticStringLifeValueDictionaryLoginedUserIdDictionary\r\n    {\r\n        get\r\n        {\r\n            returnUsableGetter(refloginedUserIdDictionary);\r\n        }\r\n    }\r\n    staticStringLifeValueDictionarypreLogoutSessionIdDictionary=null;\r\n    staticStringLifeValueDictionaryPreLogoutSessionIdDictionary\r\n    {\r\n        get\r\n        {\r\n            returnUsableGetter(refpreLogoutSessionIdDictionary);\r\n        }\r\n    }\r\n    publicSingleLoginModuler()\r\n    {\r\n    //\r\n    //TODO:在此处添加构造函数逻辑\r\n    //\r\n    }\r\n    IHttpModule成员#regionIHttpModule成员\r\n    publicvoidDispose()\r\n    {\r\n    }\r\n    publicvoidInit(HttpApplicationcontext)\r\n    {\r\n        context.PreRequestHandlerExecute+=newEventHandler(context_PreRequestHandlerExecute);\r\n        context.PostRequestHandlerExecute+=newEventHandler(context_PostRequestHandlerExecute);\r\n    }\r\n    voidcontext_PreRequestHandlerExecute(objectsender,EventArgse)\r\n    {\r\n        HttpApplicationcontext=senderasHttpApplication;\r\n        IHttpHandlerhttpHandler=context.Context.CurrentHandler;\r\n        ISingleLoginsl=httpHandlerasISingleLogin;\r\n        if(sl!=null)\r\n        {\r\n            stringsuid=sl.SigleUserLoginId;\r\n            if(suid!=string.Empty)\r\n            {\r\n                if(PreLogoutSessionIdDictionary.ContainsKey(context.Session.SessionID))\r\n                {\r\n                    //这个用户应该强制登出\r\n                    PreLogoutSessionIdDictionary.Remove(context.Session.SessionID);\r\n                    Pagepage=(Page)httpHandler;\r\n                    page.PreInit+=newEventHandler(page_PreInit);\r\n                }\r\n                elseif(!LoginedUserIdDictionary.ContainsKey(suid))\r\n                {\r\n                    LoginedUserIdDictionary.Add(suid,newLifeValue(context.Session.SessionID));\r\n                }\r\n            }\r\n        }\r\n    }\r\n    voidpage_PreInit(objectsender,EventArgse)\r\n    {\r\n        Pagepage=senderasPage;\r\n        ISingleLoginsl=pageasISingleLogin;\r\n        if(sl!=null)\r\n        {\r\n            sl.SigleUserLogout();\r\n            page.Response.End();\r\n        }\r\n    }\r\n    voidcontext_PostRequestHandlerExecute(objectsender,EventArgse)\r\n    {\r\n        //从LogineduserId里找到和当前用户一样的用户ID的SessionId\r\n        HttpApplicationcontext=senderasHttpApplication;\r\n        IHttpHandlerhttpHandler=context.Context.CurrentHandler;\r\n        ISingleLoginsl=httpHandlerasISingleLogin;\r\n        if(sl!=null)\r\n        {\r\n            stringsuid=sl.SigleUserLoginId;\r\n            if(suid!=string.Empty)\r\n            {\r\n                if(LoginedUserIdDictionary.ContainsKey(suid))\r\n                {\r\n                    stringsessionId=LoginedUserIdDictionary[suid].value;\r\n                    if(sessionId!=context.Session.SessionID)\r\n                    {\r\n                        if(!PreLogoutSessionIdDictionary.ContainsKey(sessionId))\r\n                        {\r\n                            PreLogoutSessionIdDictionary.Add(sessionId,newLifeValue(suid));\r\n                        }\r\n                        LoginedUserIdDictionary.Remove(suid);\r\n                    }\r\n                }\r\n                else\r\n                {\r\n                    LoginedUserIdDictionary.Add(sl.SigleUserLoginId,newLifeValue(context.Session.SessionID));\r\n                }\r\n            }\r\n        }\r\n    }\r\n    #endregion\r\n    }\r\n    publicclassLifeValue\r\n    {\r\n        publicstringvalue;\r\n        publicDateTimelife;\r\n        publicLifeValue(stringvalue)\r\n        {\r\n            this.value=value;\r\n            this.life=DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout+5);\r\n        }\r\n    }\r\n    publicclassStringLifeValueDictionary:Dictionarystring,LifeValue\r\n    {\r\n    }\r\n    publicinterfaceISingleLogin\r\n    {\r\n        stringSigleUserLoginId{get;}\r\n        voidSigleUserLogout();\r\n    }\r\n}


如此,只在在你自己的BasePage中改动相关的代码(只两三行)就可以实现功能了。