Failed to save asp.net cookie.

Could you tell me what I am doing wrong here. Why cookies are not saved when the page reloads:

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // it is always null !!!! if (Response.Cookies["user_id"].Value != null) { //code never gets here } } } 

and this is the code for storing the cookie (after clicking on the check box):

 protected void CheckBoxRememberMe_Click(object sender, EventArgs e) { Response.Cookies["user_id"].Value = tbUserID.Text; Response.Cookies["user_id"].Expires = DateTime.Now.AddDays(15); } 

So: I click on the checkbox, the value of the tbUserID text field is stored in the HttpCookie, then I reload the page (update), and the value is null.

Any idea?

+4
source share
1 answer

When checking a cookie, you want to make a request, not add a cookie to the response.

  if (Request.Cookies["user_id"].Value != null) { //code should get here } 
+8
source

All Articles