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"];
}
if(currentUser != null && HttpContext.Current.Session["LoginUser"] == null){
//沒有登陸的使用者,自動讀取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{
}
}else{
//匿名使用者
if(!Common.Configs.ForumConfig.SiteConfiger.AnonymousFlag)
throw new CtyException("網站不允許匿名使用者通路",DealType.RediretErrorPage);
return currentUser;
}set{
Sessioner.Add("LoginUser",value);
currentUser = value;
}
}
本文轉自快樂就好部落格園部落格,原文連結:http://www.cnblogs.com/happyday56/archive/2007/11/02/946333.html,如需轉載請自行聯系原作者