开发者

Here is some code that makes a cookie, can you please show me how to unmake it?

开发者 https://www.devze.com 2022-12-11 10:54 出处:网络
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"].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.C开发者_如何学JAVAurrent.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);


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


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.


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


System.Web.HttpContext.Current.Response.Cookies.Remove("ssocookies");
0

精彩评论

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