开发者

This .NET code snippet will NOT actually create a cookie, right?

开发者 https://www.devze.com 2022-12-28 22:41 出处:网络
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??

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 Create开发者_运维技巧SSOCookies(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' ?


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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消