---
title: "asp.net中显示在线用户"
author: "海淀区注册个昵称真难"
date: "2009-09-14T13:21:00Z"
tags: []
slug: "asp-net"
draft: false
lastmod: "2012-10-29T14:32:32Z"
id: 1707321
url: "https://201208274153.iteye.com/blog/1707321"
---

[asp.net中显示在线用户](http://www.cnblogs.com/jirong/articles/367218.html)

private static System.Threading.Timer timer;  
private const int interval = 1000 * 60 * 20;//检查在线用户的间隔时间

/// &lt;summary&gt;  
/// 必需的设计器变量。  
/// &lt;/summary&gt;  
private System.ComponentModel.IContainer components = null;  
public Global()  
{  
InitializeComponent();  
}

protected void Application\_Start(Object sender, EventArgs e)  
{  
if (timer == null)  
timer = new System.Threading.Timer(new System.Threading.TimerCallback(ScheduledWorkCallback),  
sender, 0, interval);  
DataTable userTable = new DataTable();  
userTable.Columns.Add("UserID");//用户ID  
userTable.Columns.Add("UserName");//用户姓名  
userTable.Columns.Add("FirstRequestTime");//第一次请求的时间  
userTable.Columns.Add("LastRequestTime");//最后一次请求的时间  
userTable.Columns.Add("ClientIP");//  
userTable.Columns.Add("ClientName");//  
userTable.Columns.Add("ClientAgent");//

userTable.PrimaryKey = new DataColumn\[]{userTable.Columns\[0]};  
userTable.AcceptChanges();  
Application.Lock();  
Application\["UserOnLine"] = userTable;  
Application.UnLock();  
}

protected void Session\_Start(Object sender, EventArgs e)  
{  
}  
protected void Application\_BeginRequest(Object sender, EventArgs e)  
{

}  
protected void Application\_EndRequest(Object sender, EventArgs e)  
{  
}  
protected void Application\_AcquireRequestState(Object sender, EventArgs e)  
{  
if(sender == null) return;  
if(!(sender is HttpApplication)) return;  
HttpApplication mApp = (HttpApplication)sender;  
if(mApp.Context.Session == null) return;  
if(mApp.Context.Session\["UserID"]==null ) return;  
string userID = mApp.Context.Session\["UserID"].ToString();

if(Application\["UserOnLine"] == null) return;  
if(!(Application\["UserOnLine"] is DataTable)) return;

DataTable userTable = (DataTable)Application\["UserOnLine"];  
DataRow curRow = userTable.Rows.Find(new object\[]{userID});  
if(curRow != null)  
{  
this.GetDataRowFromHttpApp(mApp,ref curRow);  
}  
else  
{  
DataRow newRow = userTable.NewRow();  
this.GetDataRowFromHttpApp(mApp,ref newRow);  
userTable.Rows.Add(newRow);  
}  
if(userTable.GetChanges()!=null)  
{  
userTable.AcceptChanges();  
Application.Lock();  
Application\["UserOnLine"] = userTable;  
Application.UnLock();  
}  
}

protected void Application\_AuthenticateRequest(Object sender, EventArgs e)  
{

}  
protected void Application\_Error(Object sender, EventArgs e)  
{

}

protected void Session\_End(Object sender, EventArgs e)  
{

}  
protected void Application\_End(Object sender, EventArgs e)  
{

}

#region Web 窗体设计器生成的代码  
/// &lt;summary&gt;  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改  
/// 此方法的内容。  
/// &lt;/summary&gt;  
private void InitializeComponent()  
{  
this.components = new System.ComponentModel.Container();  
}  
#endregion  
private void GetDataRowFromHttpApp(HttpApplication mApp,ref DataRow mRow)  
{  
if(mApp.Context.Session == null) return;  
if(mApp.Context.Session\["UserID"]==null || mApp.Context.Session\["UserName"]==null) return;  
string userID = mApp.Context.Session\["UserID"].ToString();  
string userName = mApp.Context.Session\["UserName"].ToString();  
if(mRow.RowState == DataRowState.Detached)  
{  
mRow\["UserID"] = userID;  
mRow\["UserName"] = userName;  
mRow\["FirstRequestTime"] = System.DateTime.Now;  
mRow\["ClientIP"] = mApp.Context.Request.UserHostAddress;  
mRow\["ClientName"] = mApp.Context.Request.UserHostName;  
mRow\["ClientAgent"] = mApp.Context.Request.UserAgent;  
mRow\["LastRequestTime"] = System.DateTime.Now;  
}  
else if(!mRow.IsNull("LastRequestTime"))  
{  
DateTime time = Convert.ToDateTime(mRow\["LastRequestTime"] );  
if(((TimeSpan)(DateTime.Now - time)).TotalMinutes&gt;1)  
mRow\["LastRequestTime"] = System.DateTime.Now;  
}  
}  
private void ScheduledWorkCallback (object sender)  
{  
string filter = "Convert(LastRequestTime,'System.DateTime') &lt; Convert('" + System.DateTime.Now.AddSeconds(-interval/1000).ToString() + "','System.DateTime')";  
if(Application\["UserOnLine"] == null)  
return;  
DataTable userTable = (DataTable)Application\["UserOnLine"];  
DataRow\[] lineOutUsers = userTable.Select(filter);  
for(int i=0;i&lt;lineOutUsers.Length;i++)  
{  
DataRow curRow = lineOutUsers\[i];

//保存到数据库...  
}  
if(userTable.GetChanges()!=null)  
{  
userTable.AcceptChanges();  
Application.Lock();  
Application\["UserOnLine"] = userTable;  
Application.UnLock();  
}  
}  
}