signlog 登陆实现
点击次数:17 次 发布日期:2008-11-26 23:38:17 作者:源代码网
|
源代码网推荐
关于重新登陆! 源代码网推荐1:在Global中的设置。 源代码网推荐 //添加Init,并且添加AcquireRequestState事件; 源代码网推荐 public void Init(HttpApplication application) 源代码网推荐 { 源代码网推荐 application.AcquireRequestState +=new EventHandler(application_AcquireRequestState); 源代码网推荐 源代码网推荐 } 源代码网推荐 //实现AcquireRequestState,在每次客户端response的时候都会执行这个事件; 源代码网推荐 private void application_AcquireRequestState(object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 System.Web.HttpApplication App = ((HttpApplication)sender); 源代码网推荐 if(App.Context.Session == null) return; 源代码网推荐 if(App.Context.Session["userID"] == null ) return; 源代码网推荐 System.Data.DataTable dt = (System.Data.DataTable)Application["userTable"]; 源代码网推荐 if(dt.Select("userID = "+Session["userID"].ToString()).Length>0) 源代码网推荐 { 源代码网推荐 dt.Rows[0]["loginTime"] = System.DateTime.Now; 源代码网推荐 dt.AcceptChanges(); 源代码网推荐 } 源代码网推荐
} 源代码网推荐 源代码网推荐 //Timer的间隔时间 源代码网推荐 private int interval = 20; 源代码网推荐 源代码网推荐 //在Application_Start中 1:建立在线全局用户表; 2:注册timer事件(用于间隔一定时间来维护在线用户表)。 源代码网推荐 protected void Application_Start(Object sender, EventArgs e) 源代码网推荐 { 源代码网推荐 //--1 application user table; 源代码网推荐 System.Data.DataTable dt = new DataTable(); 源代码网推荐 dt.Columns.Add("userID"); 源代码网推荐 dt.Columns.Add("loginTime"); 源代码网推荐 dt.PrimaryKey = new System.Data.DataColumn[]{dt.Columns["userID"]}; 源代码网推荐 dt.AcceptChanges(); 源代码网推荐 Application.Lock(); 源代码网推荐 Application["userTable"] = dt; 源代码网推荐 Application.UnLock(); 源代码网推荐 //--2 Timer 源代码网推荐 System.Timers.Timer tm = new System.Timers.Timer(); 源代码网推荐 tm.Interval = 60000*this.interval; 源代码网推荐 tm.Elapsed +=new System.Timers.ElapsedEventHandler(tm_Elapsed); 源代码网推荐 tm.Start(); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 //timer事件; 源代码网推荐 private void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 源代码网推荐 { 源代码网推荐 deleteTimeOutUser(); 源代码网推荐 } 源代码网推荐 //删除过期的在线用户; 源代码网推荐 private void deleteTimeOutUser() 源代码网推荐 { 源代码网推荐 if(Application["userTable"] == null) return; 源代码网推荐 System.Data.DataTable dt = (System.Data.DataTable)Application["userTable"]; 源代码网推荐 foreach(System.Data.DataRow dr in dt.Rows) 源代码网推荐 { 源代码网推荐 if(System.DateTime.Compare(((System.DateTime)dr["loginTime"]).AddMinutes(2),System.DateTime.Now)<0) 源代码网推荐 dr.Delete(); 源代码网推荐 } 源代码网推荐 dt.AcceptChanges(); 源代码网推荐 源代码网推荐 } 源代码网推荐//------------------------------用户单击退出后--的过程----------------------------------------- 源代码网推荐//退出 源代码网推荐public void reLogin(System.Web.UI.Page currentPage) 源代码网推荐{ 源代码网推荐 if((currentPage.Session != null)&&(currentPage.Session["userID"] != null)) 源代码网推荐 { 源代码网推荐 this.deleteUser(int.Parse(currentPage.Session["userID"].ToString()),currentPage.Application); 源代码网推荐 } 源代码网推荐 源代码网推荐 currentPage.Session.Abandon(); 源代码网推荐 源代码网推荐}
//删除当前用户在application中的userID; 源代码网推荐private void deleteUser(int userID,System.Web.HttpApplicationState Application) 源代码网推荐{ 源代码网推荐 if(Application["userTable"] == null) return; 源代码网推荐 System.Data.DataTable dt = (System.Data.DataTable)Application["userTable"]; 源代码网推荐 foreach(System.Data.DataRow dr in dt.Rows) 源代码网推荐 { 源代码网推荐 if(int.Parse(dr["userID"].ToString()) == userID) 源代码网推荐 dr.Delete(); 源代码网推荐 } 源代码网推荐 dt.AcceptChanges();
}
在删除在线用户时如果程序使用了Form认证模式,还应该System.Web.Security.FormsAuthentication.Signout()
---------------------------------------------------------------------------- 源代码网推荐遗留问题,虽然解决了超时注销的问题,但用户直接退出IE时如何向服务器发出 退出请求,删除当前用户ID;
源代码网供稿. |