Session和Cookie的一些基本操作

1、在session里面添加值:

Session.Add( " provider " , Doker.WebSite.AppCode.Global.CurrentUser.UserName);

获取session里的值
if  (Session[ " provider " !=   null
{
         
string provider =  Session["provider"].ToString();
}
删除session
Session.Abandon();
Session.Clear();
在Web.config里面的session的设置
     < sessionState mode = " InProc "  stateConnectionString = " tcpip=127.0.0.1:42424 "  sqlConnectionString = " data source=127.0.0.1;Trusted_Connection=yes "  cookieless = " false "  timeout = " 50 " />
      我用session保存用户权限,我的系统是一个上传大文件,一般是50M,为什么在本地测试session都不会场丢失,而一放到服务器上,用户反应上传大文件里,系统很容易退出?希待网友,帮我解决,先谢谢!于是我决定改成cookie保存用户的权限!
 2、给cookie赋值
                    HttpCookie aCookie  =   new  HttpCookie( " provider " );
                    aCookie.Value 
=  Doker.WebSite.AppCode.Global.CurrentUser.UserName;
                    aCookie.Expires 
=  DateTime.Now.AddDays( 1 );
                    Response.Cookies.Add(aCookie);
获取cookie的值
                 if  (Request.Cookies[ " provider " !=   null
                
{
                    
string provider = Server.HtmlEncode(Request.Cookies["provider"].Value); 
}
  
删除cookie的值
            HttpCookie aCookie;
            
string  cookieName;
            
int  limit  =  Request.Cookies.Count;
            
for  ( int  i  =   0 ; i  <  limit; i ++ )
            
{
                cookieName 
= Request.Cookies[i].Name;
                aCookie 
= new HttpCookie(cookieName);
                aCookie.Expires 
= DateTime.Now.AddDays(-1);
                Response.Cookies.Add(aCookie);
            }

你可能感兴趣的:(session)