views:

110

answers:

4

I just realized that this cookie is not showing up like it should, and I checked the code which was not written by me but I am pretty sure that this is NOT enough to create a cookie right??

public static void CreateSSOCookies(string tokenID)
            {
                System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Domain = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString().ToLower();
                System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Value = tokenID.ToString();
                System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Path = "~/";
                System.Web.HttpContext.Current.Response.Cookies["ssocookies"].Expires = DateTime.Now.AddDays(7);
            }

If it does work, where is the cookie then? Is the cookie name 'ssocookies' ?

A: 
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie); //<<<<<<<<<-------------------

http://msdn.microsoft.com/en-us/library/78c837bd(v=VS.80).aspx

Raj Kaimal
See that's what I thought! So this code then, is NOT creating a cookie!
shogun
It's not. I think you will get a nullreference exception since the cookie does not exist and th indexer will return null
Athens
You are both wrong, unfortunately... I was somewhat surprised myself.
Thorarin
+4  A: 

I must admit I didn't know, but apparently it does create a cookie. I've tested it, it works.

See http://msdn.microsoft.com/en-us/library/78c837bd.aspx

So far I had always used the new HttpCookie() method, which seems much .NET-like to me than a collection magically adding a cookie with the right name on first reference. I would still recommend being more explicit about creating the cookie like that, especially seeing some of the incorrect answers here :)

Edit: The path "~/" is indeed probably not what you want. Use

// Removed some of the current context stuff for readability
Response.Cookies["ssocookies"].Path = VirtualPathUtility.ToAbsolute("~");

instead.

Thorarin
I didnt know that either. I was certain you would get a nullreferenceexception.
Athens
ohhh word, that helps, thank you
shogun
Did not know that. Thanks.
Raj Kaimal
+3  A: 

I think David, commenting on the question, is correct, but to expand on his comment:

The "~/" bit is specific to ASP.NET and won't resolve the path you'd expect. Therefore, the cookie is actually being created, but since you're setting the path to something invalid, it isn't getting returned back to you.

For example, if you set the path to "/foo", the cookie would only be returned on a request to the path /foo in your application.

Since there is no absolute path in your application equal to the literal ~/, the cookie won't be returned.

Daniel Schaffer
+2  A: 

It does create a cookie. Looking in reflector, your code above is calling this:

public HttpCookie this[string name]
{
    get
    {
            return this.Get(name);
    }
}

which in turn calls:

public HttpCookie Get(string name)
{
    HttpCookie cookie = (HttpCookie) base.BaseGet(name);
    if ((cookie == null) && (this._response != null))
    {
        cookie = new HttpCookie(name);
        this.AddCookie(cookie, true);
        this._response.OnCookieAdd(cookie);
    }
    return cookie;
}

And you can see that it, in fact does, create a cookie. If you are not seeing it come back in the request, I think it has to do with your path. I am not sure "~/" is valid.

Joe