using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Cache;
using QMFrameWork.ServiceInterface;
namespace QMFrameWork.ServiceLibrary
{
///
/// Session管理
///
public class SessionManager
{
///
/// 用户登录列表
///
private static Dictionary UserLoginList = new Dictionary();
#region 判断用户是否为重复登录
///
/// 判断用户是否为重复登录
///
///
///
///
public static bool IsRepeatLogin(string userName, CredentialInfo loginc)
{
if (UserLoginList.ContainsKey(userName) == true)
{
if (UserLoginList[userName].ClientIP != loginc.ClientIP)
return false;
}
return true;
}
#endregion
#region 添加session
///
/// 添加session
///
/// 键
/// session
public static void Add(string key, ServiceSession session)
{
if (UserLoginList.ContainsKey(session.UserCredential.UserName) == true)
{
UserLoginList[session.UserCredential.UserName] = session.UserCredential;
}
else
{
UserLoginList.Add(session.UserCredential.UserName, session.UserCredential);
}
new CacheManager().Add(key, session);
}
///
/// 添加session
///
/// 键
/// session
/// 过期时间
public static void Add(string key, ServiceSession session, TimeSpan ts)
{
if (UserLoginList.ContainsKey(session.UserCredential.UserName) == true)
{
UserLoginList[session.UserCredential.UserName] = session.UserCredential;
}
else
{
UserLoginList.Add(session.UserCredential.UserName, session.UserCredential);
}
new CacheManager().Add(key, session, ts);
}
#endregion
#region 判断session是否存在
///
/// 判断session是否存在
///
/// 键
/// true:存在;false:不存在
public static bool Exists(string key)
{
object session = new CacheManager().Get(key);
if (session == null)
return false;
else
return true;
}
#endregion
#region 移除session
///
/// 移除session
///
/// 键
public static void Remove(string key)
{
ServiceSession session = GetSession(key);
if (UserLoginList.ContainsKey(session.UserLogin.UserName) == true)
UserLoginList.Remove(session.UserLogin.UserName);
new CacheManager().Remove(key);
}
#endregion
#region 获取session
///
/// 获取session
///
/// 键
/// session
public static ServiceSession GetSession(string key)
{
return new CacheManager().Get(key) as ServiceSession;
}
#endregion
#region 清除过期用户登录信息
///
/// 清除过期用户登录信息
///
public static void ClearUserList()
{
List list = UserLoginList.Values.ToList();
foreach (CredentialInfo c in list)
{
bool isExists = Exists(c.CredentialID);
if (isExists == false)
{
//对应session已过期
UserLoginList.Remove(c.UserName);
}
}
}
#endregion
}
}