signlog 登陸完成
發(fā)表時(shí)間:2024-06-13 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]關(guān)于重新登陸!1:在Global中的設(shè)置。 //添加Init,并且添加AcquireRequestState事件; public void Init(HttpApplication application) application.AcquireRequestState +=new E...
關(guān)于重新登陸!
1:在Global中的設(shè)置。
//添加Init,并且添加AcquireRequestState事件;
public void Init(HttpApplication application)
{
application.AcquireRequestState +=new EventHandler(application_AcquireRequestState);
}
//實(shí)現(xiàn)AcquireRequestState,在每次客戶端response的時(shí)候都會(huì)執(zhí)行這個(gè)事件;
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的間隔時(shí)間
private int interval = 20;
//在Application_Start中 1:建立在線全局用戶表; 2:注冊(cè)timer事件(用于間隔一定時(shí)間來維護(hù)在線用戶表)。
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();
}
//刪除當(dāng)前用戶在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();
}
在刪除在線用戶時(shí)如果程序使用了Form認(rèn)證模式,還應(yīng)該System.Web.Security.FormsAuthentication.Signout()
----------------------------------------------------------------------------
遺留問題,雖然解決了超時(shí)注銷的問題,但用戶直接退出IE時(shí)如何向服務(wù)器發(fā)出 退出請(qǐng)求,刪除當(dāng)前用戶ID;