天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.3 KiB

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