2010-02-22 66 views
0

我想用session对象在我的web app.I要存储一些饼干太(一些定制的信息)我。怎么可以没有不被修改像 http://www.example.com/(S(lit3py55t21z5v55vlm25s55))/orderform.aspxASP.NET如何一起使用SESSION和Cookies?

在我的ASP.NET页面的URL同时使用, 我设置一些会话变量

Session["customerId"]="Some name"; 

然后我试图在饼干

 HttpCookie objCookie = new HttpCookie("vendorName"); 
     Response.Cookies.Clear(); 
     Response.Cookies.Add(objCookie); 
     objCookie.Values.Add(cookiename, "ATT"); 
     DateTime dtExpiry = DateTime.Now.AddDays(2); 
     Response.Cookies[cookiename].Expires = dtExpiry; 

设置一些价值在这个页面,现在我可以访问日eseion变量值,但是当我被重定向到另一个asp.net页面时,我没有得到我的会话值。它似乎被丢失了。

任何想法如何解决这个问题。我想同时使用会话和Cookie

+2

不使用Cookie会话去的目标页面。检查你的web.config,以确保你没有启用那里的东西。 – 2010-02-22 16:58:16

回答

1

我认为你的问题可能是这个

Response.Cookies.Clear(); 

如果清除所有的cookies,您将在清除ASP.Net用来存储会话标识符的cookie。如果没有该cookie,ASP.Net无法将用户会话连接到后续请求,因此会话将丢失。

+0

会话将一直存在,直到它超时,但不会属于任何人,对吗? – 2011-05-03 08:57:48

+0

@Nick True。会话仍然会消耗服务器上的资源,直到IIS在超时期限(通常为20分钟)之后清理它, – 2011-06-21 18:55:29

4

Cookie和会话变量彼此独立。您可能会感到困惑,因为默认情况下,Asp.Net Sessions使用cookie来存储会话标识符,并且在Cookie被禁用时,Asp.Net会将会话标识符置于URL中可见的查询字符串值中。

使用cookie为此

// Set the value in a response 
Response.Cookies["SomeCookieVar"].Value = "SomethingImportant"; 

// After a post back read the value from the request 
Request.Cookies["SomeCookieVar"].Value; 

Session变量都是这样

// Set the value 
Session["SomeSessionVar"] = "SomethingElse"; 
// Read the value 
String SomeSetting = Session["SomeSessionVar"]; 

这是假设你是在C#里面工作,ASPX页面类访问。 VB.Net语法略有不同,http处理程序和模块需要您做一些工作才能获得请求,响应和会话。

会话变量和Cookies值都可以混合并与您的心内容相匹配,不会造成任何冲突。一种常见的情况是将值存储在您希望持续存在不同会话的cookie中。但为了使其发挥作用,您必须在您的Cookie上设置过期时间。没有到期的Cookies不是持久性的,不会在浏览器会话之间持续。

// make the cookie to last for a week 
Request.Cookies["SomeCookieVar"].Expiration = DateTime.Now().AddDays(7); 
0

您可以考虑使用这个小库:

http://www.codeproject.com/KB/aspnet/Univar.aspx

每当Cookie不可用它可以自动切换到会议。它还具有cookie的服务器端实现,从而所有cookie都存储在服务器上,并且可以使用asp.net认证来识别用户。

0
protected void btnSend_Click(object sender, EventArgs e) 
    { 
     // declaring a HttpCookies here with a Parameter 
     HttpCookie Cookies = new HttpCookie("Name"); 

     //Clearing the Cookies 
     Response.Cookies.Clear(); 

     // Set a value in it. 
     // here the Cookies object act like a type and inside the'[" "]' is a  Cookie's variable 
     Cookies["ID"] = txtID.Text; 
     Cookies["MyName"] = txtName.Text; 
     Cookies["Contact_No"] = txtContactNo.Text; 
     Cookies["EmailID"] = txtEmailID.Text; 

     // Add it to the current web response. 
     Response.Cookies.Add(Cookies); 

     //Setting the Exparation Date For he Cookies 
     HttpCookie Cookies1 = new HttpCookie("Expiration"); 
     Cookies1.Expires = DateTime.Now.AddDays(-1); 

     //Redirecting to another page 
     Response.Redirect("Accepting_Cookies_Details.aspx"); 
} 

//您要在默认情况下asp.net检索您的饼干的价值观

protected void Page_Load(object sender, EventArgs e) 
    { 
     HttpCookie Cookie = Request.Cookies["Name"]; 
     string ID1 = Cookie["ID"]; 
     string MyName1 = Cookie["MyName"]; 
     string Contact_No1 = Cookie["Contact_No"]; 
     string EmailID1 = Cookie["EmailID"]; 
     Literal1.Text = ID1; 
     Literal2.Text = MyName1; 
     Literal3.Text = Contact_No1; 
     Literal4.Text = EmailID1; 
    }