tags:

views:

344

answers:

4
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);

Now what code would I do later on in my web app when the user clicks logout to make that cookie get destroyed?

NOTE I TRIED THIS ALREADY WITH AND WITHOUT THE COMMENTED LINES AND IT DOESN'T WORK:

    //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(-1);
+5  A: 

What I do is set it again, with a blank value and an expiry date in the past:

var context = System.Web.HttpContext.Current; 

context.Response.Cookies["ssocookies"].Domain = context.Request.ServerVariables["SERVER_NAME"].ToString().ToLower();
context.Response.Cookies["ssocookies"].Value = "";
context.Response.Cookies["ssocookies"].Path = "~/";
context.Response.Cookies["ssocookies"].Expires = DateTime.Now.AddDays(-1);
Garry Shutler
+1 Thats a good idea!
Preet Sangha
see my new edit on question
shogun
It should work, I would check how the browser is actually storing the cookie. Check it's not getting mangled along the way.
Garry Shutler
will try again and post back...
shogun
+2  A: 

Expire cookies by setting their expiration time in the past.

        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);
Joshua
tried that, failed to work
shogun
A: 
System.Web.HttpContext.Current.Response.Cookies.Remove("ssocookies");
Alex
+2  A: 

The method FormsAuthentication.SignOut does it something like:

   HttpCookie cookie = new HttpCookie(FormsCookieName, str);
    cookie.HttpOnly = true;
    cookie.Path = _FormsCookiePath;
    cookie.Expires = new DateTime(1999, 10, 12);
    cookie.Secure = _RequireSSL;
    if (_CookieDomain != null)
    {
        cookie.Domain = _CookieDomain;
    }
    current.Response.Cookies.RemoveCookie(FormsCookieName);
    current.Response.Cookies.Add(cookie);

In any case, using something like Fiddler to inspect your http traffic should give you a clue as to what's going on.

Joe