高效的Session读写.
作者:ym110 日期:2007-04-06
private static User currentUser = null;

/**//// <summary>
/// 当前用户
/// </summary>

public static User CurrentUser
{

get
{
if(currentUser != null && HttpContext.Current.Session["LoginUser"] != null)
return currentUser;

if(HttpContext.Current.Session["LoginUser"] != null)
{
currentUser = (User)HttpContext.Current.Session["LoginUser"];
return currentUser;
}


if(currentUser != null && HttpContext.Current.Session["LoginUser"] == null)
{
return currentUser;
}
//没有登陆的用户,自动读取cookie登陆
currentUser = new PopForum.Common.Entity.User();
HttpCookie c = HttpContext.Current.Request.Cookies["UserInfo"];

if(c!=null && c.Value!="")
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(c.Value);
Common.DataMapping.UserInfo info = null;
Users users = new Users();
int r = users.CheckUser(ticket.Name,out info);

if(r==1)
{
currentUser.UserId = info.UserId;
currentUser.UserName = info.UserName;
currentUser.Status = (Common.UserStatus)info.UserStatus;
currentUser.Point = info.Point;
DataTable table = DataBase.ExecuteSQLTable("select rr.rightid from t_roleright rr,t_userrole ur where rr.roleid=ur.roleid and ur.userid="+currentUser.UserId.ToString());

foreach(DataRow row in table.Rows)
{
currentUser.Rights.Add((Common.Right)row["rightid"]);
}
//判断用户是否有系统管理权限

if(currentUser.HaveRight(Common.Right.SiteAdmin))
{
//更新用户登陆信息
users.LoginUpdate(ref info,currentUser);
currentUser.Point = info.Point;
Sessioner.Add("LoginUser",currentUser);
}
//判断网站是否关闭,但管理员可以登陆

else if(Common.Configs.ForumConfig.SiteConfiger.CloseFlag)
{
throw new CtyException("网站被关闭,暂时无法访问",DealType.RediretErrorPage);
}
// //判断是否允许登陆
// else if(!Common.Configs.ForumConfig.SiteConfiger.LoginFlag){
// throw new CtyException("登陆被禁止,暂时无法登陆",DealType.RediretErrorPage);
// }

else
{
users.LoginUpdate(ref info,currentUser);
currentUser.Point = info.Point;
Sessioner.Add("LoginUser",currentUser);
}
}

}else
{
//匿名用户
if(!Common.Configs.ForumConfig.SiteConfiger.AnonymousFlag)
throw new CtyException("网站不允许匿名用户访问",DealType.RediretErrorPage);
}
return currentUser;

}set
{
Sessioner.Add("LoginUser",value);
currentUser = value;
}
}

/**//// <summary>
/// 当前用户
/// </summary>
public static User CurrentUser
{
get
{
if(currentUser != null && HttpContext.Current.Session["LoginUser"] != null)
return currentUser;

if(HttpContext.Current.Session["LoginUser"] != null)
{
currentUser = (User)HttpContext.Current.Session["LoginUser"];
return currentUser;
}

if(currentUser != null && HttpContext.Current.Session["LoginUser"] == null)
{
return currentUser;
}
//没有登陆的用户,自动读取cookie登陆
currentUser = new PopForum.Common.Entity.User();
HttpCookie c = HttpContext.Current.Request.Cookies["UserInfo"];
if(c!=null && c.Value!="")
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(c.Value);
Common.DataMapping.UserInfo info = null;
Users users = new Users();
int r = users.CheckUser(ticket.Name,out info);
if(r==1)
{
currentUser.UserId = info.UserId;
currentUser.UserName = info.UserName;
currentUser.Status = (Common.UserStatus)info.UserStatus;
currentUser.Point = info.Point;
DataTable table = DataBase.ExecuteSQLTable("select rr.rightid from t_roleright rr,t_userrole ur where rr.roleid=ur.roleid and ur.userid="+currentUser.UserId.ToString());
foreach(DataRow row in table.Rows)
{
currentUser.Rights.Add((Common.Right)row["rightid"]);
}
//判断用户是否有系统管理权限
if(currentUser.HaveRight(Common.Right.SiteAdmin))
{
//更新用户登陆信息
users.LoginUpdate(ref info,currentUser);
currentUser.Point = info.Point;
Sessioner.Add("LoginUser",currentUser);
}
//判断网站是否关闭,但管理员可以登陆
else if(Common.Configs.ForumConfig.SiteConfiger.CloseFlag)
{
throw new CtyException("网站被关闭,暂时无法访问",DealType.RediretErrorPage);
}
// //判断是否允许登陆
// else if(!Common.Configs.ForumConfig.SiteConfiger.LoginFlag){
// throw new CtyException("登陆被禁止,暂时无法登陆",DealType.RediretErrorPage);
// }
else
{
users.LoginUpdate(ref info,currentUser);
currentUser.Point = info.Point;
Sessioner.Add("LoginUser",currentUser);
}
}
}else
{
//匿名用户
if(!Common.Configs.ForumConfig.SiteConfiger.AnonymousFlag)
throw new CtyException("网站不允许匿名用户访问",DealType.RediretErrorPage);
}
return currentUser;
}set
{
Sessioner.Add("LoginUser",value);
currentUser = value;
}
}
Tags: Session
- 1






