tags:

views:

48

answers:

1

I have a static class in my solution that is basically use a helper/ultility class.

In it I have the following static method:

// Set the user
    public static void SetUser(string FirstName, string LastName)
    {
        User NewUser = new User { Name = String.Format("{0}{1}", FirstName, LastName) };
        HttpCookie UserName = new HttpCookie("PressureName") { Value = NewUser.Name, Expires = DateTime.Now.AddMinutes(60) };       

    }

User is a simple class that contains:

  String _name = string.Empty;

    public String Name
    {
        get { return _name; }
        set { _name = value; }
    }

Everything works up until the point where I try to write the cookie "PressureName" and insert the value in it from NewUser.Name. From stepping through the code it appears that the cookie is never being written.

Am I making an obvious mistake? I'm still very amateur at c# and any help would be greatly appreciated.

+3  A: 

Creating the cookie object is not enough to have it sent to the browser. You have to add it to the Response object also.

As you are in a static method, you don't have direct access to the page context and it's Response property. Use the Current property to access the Context of the current page from the static method:

HttpContext.Current.Response.Cookies.Add(UserName);
Guffa
Thank you sir, I appreciate the help!
Strategon