高效的Session讀寫
private static User currentUser = null;
/**////
/// 當前用戶
///
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;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.