/*----------------------------------------------------------------
// Copyright (C) 公司名称
// 版权所有。
//
// 文件名:LoginInfor.cs UserLogin.cs
// 文件功能描述:登录用户信息处理类
//
//
// 创建标识:wyb 2011-9-5
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
namespace Common
{
/// <summary>
/// 登陆用户信息
/// </summary>
public class LoginInfor
{
#region File
private string _UserID;
private string _UserName;
private bool _IsAdmin;
DateTime m_Time;
#endregion
#region Property
/// <summary>
/// 员工的工号
/// </summary>
public string UserID
{
get { return _UserID; }
set { _UserID = value; }
}
/// <summary>
/// 员工的用户名
/// </summary>
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
/// <summary>
/// 员工的是否是录入管理员
/// </summary>
public bool IsAdmin
{
get { return _IsAdmin; }
set { _IsAdmin = value; }
}
public DateTime Time
{
get { return m_Time; }
set { m_Time = value; }
}
#endregion
#region Method
public LoginInfor()
{
m_Time = DateTime.Now;
}
#endregion
}
public static class UserLogin
{
//哈希表,作为保存登录用户的队列
private static Hashtable m_userList;
//用户在线超时的时限(60分钟)
private static TimeSpan m_tsSub = new TimeSpan(0, 60, 0);
public static void Init()
{
m_userList = Hashtable.Synchronized(new Hashtable());
}
public static void Empty()
{
m_userList.Clear();
}
/// <summary>
/// 登录信息在数据库中验证成功后调用
/// </summary>
/// <param name="keyName">键值</param>
/// <param name="UserName">用户名</param>
/// <param name="isManager">是否是管理员</param>
/// <returns></returns>
public static int AddUserToList(string keyName, string userID, string UserName, bool isManager)
{
int ret = 0;
try
{
LoginInfor hashElt = new LoginInfor();
hashElt.UserID = userID;
hashElt.UserName = UserName;
hashElt.IsAdmin = isManager;
//如果用户已经登录过,则只更新登录流水号及操作时间,否则新加入队列
lock (m_userList.SyncRoot)
{
if (m_userList.ContainsKey(keyName))
{
m_userList[keyName] = hashElt;
}
else
{
m_userList.Add(keyName, hashElt);
}
}
}
catch (Exception)
{
ret = -1;
}
return ret;
}
/// <summary>
/// 获取登陆用户
/// </summary>
/// <returns></returns>
public static Hashtable GetUserLogin()
{
lock (m_userList.SyncRoot)
{
lock (m_userList.SyncRoot)
{
return m_userList;
}
}
}
/// <summary>
///验证用户是否登陆
/// </summary>
/// <param name="userName">用户名</param>
/// <returns>
/// 0:验证用户登录成功
/// -1:验证用户登录失败
/// -2:用户未登录
/// -3:用户已重新登录或在别处登录
/// </returns>
public static int CheckUserLogin(string keyName)
{
int ret = 0;
try
{
lock (m_userList.SyncRoot)
{
if (m_userList.ContainsKey(keyName))
{
LoginInfor hashElt = m_userList[keyName] as LoginInfor;
if (hashElt.UserID.Equals(keyName))
{
hashElt.Time = DateTime.Now;
m_userList[keyName] = hashElt;
ret = 0;
}
else
{
ret = -3;
}
}
else
{
ret = -2;
}
}
}
catch
{
ret = -1;
}
return ret;
}
/// <summary>
/// 检查保存在队列中的用户在线是否超过时限,该方法供线程调用
/// </summary>
private static void CheckListTO()
{
try
{
//保存待清理的超时在线用户的临时队列
List<string> toKeys = new List<string>();
while (true)
{
//线程每隔1分钟执行检查清理操作
Thread.Sleep(1000 * 60);
//遍历保存在线用户的哈希表
lock (m_userList.SyncRoot)
{
foreach (DictionaryEntry deElt in m_userList)
{
//如果有超时的在线用户,将超时用户时放入临时的队列中,
//检查完哈希表之后再进行清理操作
//不能在检查过程中执行哈希表的删除键值操作,否则会出现异常。
LoginInfor htElt = deElt.Value as LoginInfor;
if (DateTime.Now - htElt.Time > m_tsSub)
{
toKeys.Add((string)deElt.Key);
}
}
//如果有超时的在线用户则进行清理操作
if (0 < toKeys.Count)
{
foreach (string key in toKeys)
{
m_userList.Remove(key);
}
toKeys.Clear();
}
}
}
}
catch
{
}
}
}
}
登陆成功后将你要保存的对象信息保存起来:比如
//登录数据存储
UserLogin.AddUserToList("user", strUserNum, model.UserName, isAdmin);
在使用登陆信息是类似于获取session对象类似:比如:
/// <summary>
/// 获取登陆员工的信息
/// </summary>
/// <returns></returns>
public static LoginInfor GetLoginUser()
{
Hashtable userLogin = UserLogin.GetUserLogin();
return (LoginInfor)userLogin["user"];
}