关于ASP.NET2.0环境中显示在线用户的办法
讨论一下关于ASP.NET2.0环境中显示在线用户的办法,因为最近工作中遇到这个问题,虽然解决了,但是办法不好,而且不想使用MEMBER SHIP,因为感觉真的很麻烦,特别是对于我们的程序,权限设计比较复杂,希望大家可以说说自己的示在线用户de好办法@@@
------解决方案--------------------帮楼主顶!
------解决方案--------------------不用MEMBER SHIP也没什么特别好的办法,在会话开始事件中在全局变量中增加1个名额,然后别人退出时在会话结束事件中在全局变量中减少一个名额。
------解决方案--------------------显示在线用户还是显示在线用户名字还是人数?
------解决方案--------------------public class MemberOnlineInfo
{
public string userName;
public string userId;
public string IP;
public DateTime lastActive;
public string lastUrl;
public MemberOnlineInfo(string name, string id, string ip, DateTime lastactive, string lasturl)
{
userName = name;
userId = id;
IP = ip;
lastActive = lastactive;
lastUrl = lasturl;
}
}
public class MemberOnlineList
{
public static Hashtable memberList = new Hashtable();
public static bool IsOnline(string name)
{
return (memberList.ContainsKey(name)) ? true : false;
}
public static void AddOnline(MemberOnlineInfo online)
{
memberList.Add(online.userName, online);
}
public static void UpdateOnline(MemberOnlineInfo online)
{
memberList[online.userName] = online;
}
public static void DeleteOnline() //超过20分钟的删除
{
DateTime now = DateTime.Now;
foreach(DictionaryEntry deChair in memberList)
{
if ((now - ((MemberOnlineInfo)(deChair.Value)).lastActive) > TimeSpan.FromMinutes(20)){
memberList.Remove(((MemberOnlineInfo)(deChair.Value)).userName);
}
}
}
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
string strName =string.Empty;
string strUid =string.Empty;
if (Request.IsAuthenticated)
{
strName = User.Identity.Name;
strUid = Request.Cookies[ "uid "].Value;
}
else {
strName = Request.Cookies[ "uid "].Value;
}
MemberOnlineInfo Online = new MemberOnlineInfo(strName, strUid, Request.UserHostAddress, DateTime.Now, Request.FilePath);
if (MemberOnlineList.IsOnline(strName))
{
MemberOnlineList.UpdateOnline(Online);
}
else
{
MemberOnlineList.AddOnline(Online);
}
MemberOnlineList.DeleteOnline();
}
//显示在线的用户
public StringBuilder online=new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
online.Append( " <table cellSpacing=\ "2\ " cellPadding=\ "2\ " width=\ "90%\ " class=\ "grid\ "> ");
online.Append( " <tr> ");
online.Append( " <th > User </th> ");
online.Append( " <th> </th> ");