How can I set cookie value in one page and read it from another page in a asp.net website

50,470

Solution 1

you basically need to request the cookie it does not really matter on what page you are here is an explanation about cookies

http://msdn.microsoft.com/en-us/library/ms178194.aspx

HttpCookie aCookie = Request.Cookies["loginCookie"];
string username = Server.HtmlEncode(aCookie.Value);

Solution 2

Your code that sets loginCookie looks strange:

HttpCookie loginCookie1 = new HttpCookie("loginCookie"); 
Response.Cookies["loginCookie1"].Value = LoginUser.UserName; // <--- strange!!!!
Response.Cookies.Add(loginCookie1); 

Most likely your cookie does not get send to browser - check with HTTP debugger like Fiddler.

Solution 3

This should do it:

var userName = Request.Cookies["loginCookie"].Value;
Share:
50,470
techblog
Author by

techblog

Updated on July 09, 2022

Comments

  • techblog
    techblog almost 2 years

    This is my code in Login.aspx

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        HttpCookie loginCookie1 = new HttpCookie("loginCookie");
        Response.Cookies["loginCookie1"].Value = LoginUser.UserName;
        Response.Cookies.Add(loginCookie1);
    }
    

    And this is in shop.aspx

    protected void btnAddCart_Click(object sender, EventArgs e)
    { 
         HttpCookie myCookie = new HttpCookie(dvProduct.DataKey.Value.ToString());
         myCookie["Category"] = dvProduct.DataKey["Category"].ToString();
         myCookie["Product"] = dvProduct.DataKey["Product"].ToString();
         myCookie["Quantity"] = txtQuantity.Text;
         myCookie["Price"] = dvProduct.DataKey["Price"].ToString();
         myCookie.Expires = DateTime.Now.AddDays(1d);
         Response.Cookies.Add(myCookie);
         Response.Redirect("ViewCart.aspx", true);
    }
    

    I want to read the value of username from cookie(value set in login.aspx

  • techblog
    techblog about 12 years
    >I am getting this error with the above code <br>{"Object reference not set to an instance of an object."}
  • techblog
    techblog about 12 years
    >I am retrieving the username from the Login control of asp.net. LoginUser is the ID of the Login control. Here I checked if it is retrieving the username, it is retrieving it fine. I am just having thr trouble reading it.
  • Alexei Levenkov
    Alexei Levenkov about 12 years
    Please post what cookie header is actuallly send to browser by "login.aspx" and from browser back in "shop.aspx" page.
  • techblog
    techblog about 12 years
    >Hi, I got the solution. Below code solved the problem. HttpCookie aCookie = Request.Cookies["loginCookie1"]; string username = Server.HtmlEncode(aCookie.Value); >Thanks.
  • Alexei Levenkov
    Alexei Levenkov about 12 years
    Good to hear. It is a way too I guess... also fixing strange code on login page to set cookie with name you expcect is probably more appropriate.
  • Arion
    Arion about 12 years
    @techblog : remember to upvote the answers you think are good. That gives us all a warm fuzzy feeling :P .. And if this answer was the resolution to your question then accept it.. I like this +1.
  • Jason M
    Jason M over 10 years
    Its because you can't get the value of a null object.