Try Now
More in this section

Forums / General Discussions / Cookie not setting on Page_Load

Cookie not setting on Page_Load

2 posts, 0 answered
  1. Beth Grabo
    Beth  Grabo avatar
    6 posts
    02 Mar 2010
    11 Aug 2010
    Link to this post
    Hi there.  I am a new asp.net coder so please help me if I am doing something wrong.  We are trying to set up a simple cookie to record which server a user is at (in our server farm) so we can tell the load balancer program to keep the user on the same server throughout their session. (our load balancer has no session logic).

    protected void Page_Load(object sender, EventArgs e)
      if (Request.Cookies["serverID"] == null) //check if cookie exists
        //if not, then grab server variable from web.config file
        string serverName = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("serverID");
        //then set cookie
        Response.Cookies["serverID"].Value = serverName;

    This seems very simple, yet, it is not working.  The only cookies being set are those by Google Analytics.  What am I doing wrong?
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    01 Feb 2016
    12 Aug 2010
    Link to this post
    Hi Beth Grabo,

    Can you try with something like the code bellow:
    if (Request.Cookies["serverID"] == null) //check if cookie exists
        System.Web.HttpCookie aCookie = new System.Web.HttpCookie("serverID");
        aCookie.Value = "server1";
        aCookie.Expires = DateTime.Now.AddDays(1);

    You can find more information on ASP.NET Cookises in the linked article.

    All the best,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered