问题描述
本人程序有以下步骤:1,自定义一个baseControllerpublicclassBaseController:Controller{publicLoginUserCustomUser=newLoginUser();publicBaseController(){CustomUser.Load();}}作用为使用CustomUser.Load();从cookies中反序列化回登录系统的个人信息;2,publicstringLogin(LoginFormusert){TospurUser.Model.Useruser=TospurUser.BLL.User.UserLogin(usert.UserName,usert.PassWord);if(user!=null){List<UserRoleProject>list=CommonProvider.GetCurrentProject(user.ID.ToString());LoginUserCustomUser=newLoginUser();CustomUser.UserId=user.ID.ToString();CustomUser.UserRoleId="1";CustomUser.PositionID=user.PositionID;CustomUser.DutyID=user.DutyID.ToString();CustomUser.FrameID=user.FrameID.ToString();CustomUser.UserName=user.Name;CustomUser.PresentFrame=user.FrameName;CustomUser.DisplayName=user.Name;CustomUser.Save();Session["DisplayName"]=CustomUser.DisplayName;Session["UserName"]=CustomUser.UserName;Session["UserId"]=CustomUser.UserId;Response.Redirect("/home/index");}else{Response.Redirect("/home/Login");}return"";}3,CustomUser类继承一个序列化对象到cookies的工具类[Serializable]publicclassCookieInfo{#region====================ConstructedMethod====================///<summary>///构造函数///</summary>publicCookieInfo(){}#endregion#region====================PublicMethod====================///<summary>///得到当前Cookies的过期时间///</summary>///<returns>过期时间</returns>publicDateTimeGetExpiresTime(){stringcookieName=GetType().ToString();if(HttpContext.Current.Request.Cookies[cookieName]!=null){returnHttpContext.Current.Request.Cookies[cookieName].Expires;}returnDateTime.MinValue;}///<summary>///保存Cookies,过期时间为浏览器关闭则失效。///</summary>///<paramname="expiresTime">Cookies过期事件</param>///<returns>是否保存成功</returns>publicboolSave(){returnthis.Save(DateTime.MinValue);}///<summary>///保存Cookies,需要指定过期时间。///</summary>///<paramname="expiresTime">Cookies过期事件</param>///<returns>是否保存成功</returns>publicboolSave(DateTimeexpiresTime){stringCookieName=GetType().ToString();HttpCookieSessionCookie=null;//对SessionId进行备份.if(HttpContext.Current.Request.Cookies["ASP.NET_SessionId"]!=null){stringSesssionId=HttpContext.Current.Request.Cookies["ASP.NET_SessionId"].Value.ToString();SessionCookie=newHttpCookie("ASP.NET_SessionId");SessionCookie.Value=SesssionId;}SessionCookie=newHttpCookie("ASP.NET_SessionId");//设定cookie过期时间.DateTimedtExpiry=expiresTime;HttpContext.Current.Response.Cookies[CookieName].Expires=dtExpiry;//设定cookie域名.stringdomain=string.Empty;if(HttpContext.Current.Request.Params["HTTP_HOST"]!=null){//domain="www.elong.com";domain=HttpContext.Current.Request.Params["HTTP_HOST"].ToString();}//如果是www.elong.com或多级域名,需要转化为elong.comif(domain.IndexOf(".")>-1){string[]temp=domain.Split('.');if(temp.Length>=3){domain=temp[temp.Length-2].Trim()+"."+temp[temp.Length-1].Trim();}HttpContext.Current.Response.Cookies[CookieName].Domain=domain;}//把类的属性,写入Cookie.PropertyInfo[]Propertys=GetType().GetProperties();foreach(PropertyInfopiinPropertys){objectoj=pi.GetValue(this,null);Typetype=pi.PropertyType;stringvalueStr=string.Empty;if(oj!=null&&oj.ToString()!=string.Empty){if(type==Type.GetType("System.DateTime")){valueStr=((DateTime)oj).ToString("yyyy/MM/ddHH:mm:ss",System.Globalization.DateTimeFormatInfo.InvariantInfo);}else{valueStr=oj.ToString();}HttpContext.Current.Response.Cookies[CookieName][pi.Name]=HttpUtility.UrlEncode(valueStr);}}//如果cookie总数超过20个,重写ASP.NET_SessionId,以防Session丢失.if(HttpContext.Current.Request.Cookies.Count>20&&SessionCookie!=null){if(SessionCookie.Value!=string.Empty){HttpContext.Current.Response.Cookies.Remove("ASP.NET_SessionId");HttpContext.Current.Response.Cookies.Add(SessionCookie);}}returntrue;}///<summary>///找回Cookie值///</summary>publicvoidLoad(){stringcookieValue=string.Empty;stringCookieName=GetType().ToString();//通过遍历属性,从cookie中找回值,回写到属性.PropertyInfo[]Propertys=GetType().GetProperties();foreach(PropertyInfopiinPropertys){try{cookieValue=HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[CookieName][pi.Name].ToString());}catch(Exceptione){cookieValue=string.Empty;}if(pi.CanWrite&&cookieValue!=null&&cookieValue!=string.Empty){try{objectobb=cookieValue;Typetype=pi.PropertyType;obb=Convert.ChangeType(obb,type);pi.SetValue(this,obb,null);}catch{}}}}#endregion