The issue of domain deletion of cookies failed

Source: Internet
Author: User
Tags set cookie

Recently took over an old project, the project was using a cookie to do the processing, added when the cookie was added to the domain,

However, the deletion did not add a domain, resulting in the deletion of cookies has failed! There are also the creation and deletion of cookies, which must be refreshed by the page, or the page will be skipped.

  and IE deleted the cookie when it was successful! such as the JS code below,        

Document.execcommand ("Clearauthenticationcache")

Tried, ie completely normal, if it is so simple to solve this problem, but also too underestimated our browser army, Firefox and Chrome and other non-Microsoft Department browser ignore the above code, so only a different way. Refer to: http://wangye.org/blog/archives/874/

<script type= "Text/javascript" > Function singout () {Deletecookie ("Weactoken"); Deletecookie ("Ltpatoken");Document.execcommand ("Clearauthenticationcache");window.location.href = ' login.aspx ';            }/* Delete cookie */function Deletecookie (name) {var expdate = new Date ();            Expdate.settime (Expdate.gettime ()-1000);        Setcookie (Name, "", expdate);            }/* Set cookie */function Setcookie (name, value) {var argv = setcookie.arguments;            var argc = setCookie.arguments.length; var expires = (argc > 2)?            ARGV[2]: null; if (! (                Expires instanceof Date) {alert ("This expires date is null!");            Return } document.cookie = name + "=" + Escape (value) + ((expires = = null)?        "": ("; expires=" + expires.togmtstring ())); } </script>

  

The cookie is removed successfully under IE, but it is not the function of the Deletecookie method, but the function of Document.execcommand ("Clearauthenticationcache"), which has been misled.

Modifications to the following code: C #

Explanation for domain: No explanation was found in the global Consortium, the explanation in MSDN is the default current domain.

Https://msdn.microsoft.com/zh-cn/library/system.web.httpcookie.domain.aspx

There are multiple subsystems common a domain name, if there are two of the same cookie name, then delete the cookie in one subsystem is deleted which one, so did not add the domain name, causing the browser does not specify which cookie to delete. (Personal explanation)

        protected voidPage_Load (Objectsender, EventArgs e) {            //session.remove ("LoginStatus");HttpCookie Eaccookie =Request.cookies[consts.cookie_name]; HttpCookie Ltpacookie= request.cookies["Ltpatoken"];            Removecookie (Eaccookie);            Removecookie (Ltpacookie); //string script = "<script>window.close ();</script>"; //Clientscript.registerstartupscript (this.            GetType (), "logout", script); //Response.Redirect ("default.aspx");        }        Private voidRemovecookie (HttpCookie cookie) {if(Cookie! =NULL{HttpContext.Current.Response.Cookies.Remove (cookie).                Name); cookies. Domain = "." + MCS. Sso. DataAccess.SettingAccess.GetSettingValue ("Ssodomain");Cookies. Value =String.Empty; Cookies. Expires= DateTime.Now.AddDays (-1);            HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookie); }        }  

Handling cookie removal like the above succeeds!

<script type= "Text/javascript" >functionsingout () {//Deletecookie ("Weactoken");            //Deletecookie ("Ltpatoken");            //Document.execcommand ("Clearauthenticationcache");window.location.href = ' Login.aspx '; }        /*Delete Cookies*/        functionDeletecookie (name) {varExpdate =NewDate (); Expdate.settime (Expdate.gettime ()-1000); Setcookie (Name,"", expdate); }        /*Set Cookies*/        functionSetcookie (name, value) {varargv =setcookie.arguments; varARGC =setCookie.arguments.length; varexpires = (argc > 2)? ARGV[2]:NULL; if(! (ExpiresinstanceofDate)) {Alert ("This expires date is null!"); return; } document.cookie= name + "=" + Escape (value) + ((expires = =NULL) ? "": ("; expires=" +expires.togmtstring ())); }     </script>

The cookie expiration Time setting succeeds, the browser clears the cookie successfully!

The issue of domain deletion of cookies failed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.