老问题了,我想写个分配和管理Session的类,有没有好的思路?
功能:创建Session、获取Session、判断Session是否过期、删除Session
对Session实现统一管理,不能在页面中直接使用Session
请高手指教一下 或 给一个比较好的思路。
[解决办法]
try
HttpModule
[解决办法]
那你再弄一个外观层 不就结了?
[解决办法]
去年写的,这样的可以满足你的要求吗?
/// <summary>
/// SessionLib µÄժҪ˵Ã÷¡£
/// </summary>
public class SessionLib
{
private HttpSessionState Session;
public SessionLib()
{
//
// TODO: ÔÚ´Ë´¦Ìí¼Ó¹¹Ô캯ÊýÂß¼­
//³õÊÔ»¯SESSION¶ÔÏñ ³¬Ê±Ê±¼äΪ60·ÖÖÓ
Session= HttpContext.Current.Session;
Session.Timeout= 20;
}
/// <summary>
/// Óû§Ö÷¼üID
/// </summary>
public long CID
{
get
{
if(isNull())
throw new Exception( "»á»°ÉÐ佨Á¢... ");
try
{
return long.Parse(Session[ "cid "].ToString());
}
catch
{
throw new Exception( "»á»°ÉÐ佨Á¢... ");
}
}
set
{
Session.Add( "cid ",value);
}
}
/// <summary>
/// ÕʺÅ
/// </summary>
public string Account
{
set
{
this.Session.Add( "cuserid ",value);
}
get
{
if(isNull())
throw new Exception( "»á»°ÉÐ佨Á¢... ");
return this.Session[ "cuserid "].ToString();
}
}
/// <summary>
/// ÃÜÂë
/// </summary>
public string PassWord
{
set
{
Session.Add( "cpassword ",value);
}
get
{
if(isNull())
throw new Exception( "»á»°ÉÐ佨Á¢... ");
return Session[ "cpassword "].ToString();
}
}
/// <summary>
/// ½ÇÉ«
/// </summary>
public string Role
{
set
{
Session.Add( "role ",value);
}
get
{
if(isNull())
throw new Exception( "»á»°ÉÐ佨Á¢... ");
return Session[ "role "].ToString();
}
}
/// <summary>
/// ȨÏÞ
/// </summary>
public long Popedom
{
set
{
Session.Add( "popedom ",value);
}
get
{
if(isNull())
throw new Exception( "»á»°ÉÐ佨Á¢... ");
try
{
return long.Parse(Session[ "popedom "].ToString());
}
catch
{
throw new Exception( "»á»°ÉÐ佨Á¢... ");
}
}
}
/// <summary>
/// ¹«Ë¾ID
/// </summary>
public long CompayID
{
set
{
Session.Add( "compayid ",value);
}
get
{
if(isNull())
throw new Exception( "»á»°ÉÐ佨Á¢... ");
try
{
return long.Parse(Session[ "compayid "].ToString());
}
catch
{
throw new Exception( "»á»°ÉÐ佨Á¢... ");
}
}
}
/// <summary>
/// »ñÈ¡SESSIONID
/// </summary>
public string SessionID
{
get
{
if(isNull())
throw new Exception( "»á»°ÉÐ佨Á¢... ");
return Session.SessionID;
}
}
/// <summary>
/// ÅжϽ¨Á¢»á»°
/// </summary>
/// <returns> bool </returns>
public bool isNull()
{
if(Session[ "cid "]==null)
return true;
else
return false;
}
/// <summary>
/// Ïú»Ù»á»°
/// </summary>
public void SessionDispose()
{
Session.Abandon();
}
[解决办法]
嗯,Session 已经过期了,现在好多web项目是基于无session的
比如petshop4.0