Clear 6 ways webbrowser cookie / session of

The following are six ways I tested six kinds of Clear webbrowser down in a cookie:

Copy the code
            // Method One: Call wininet.dll Clear cookie (recommended)
            SuppressWininetBehavior();

            // Method Two: Remove the user login information, this led to the cancellation of the function of the browser, ie using the built-in functions (recommended)
            HtmlDocument document = wb.Document;
            document.ExecCommand("ClearAuthenticationCache", false, null);

            // Method three: Delete local cookie This method will pop up a pop-up ie remove the cookie
            // Temporary Internet Files (Internet temporary files)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
            //Cookies
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
            // History (History)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
            // Form. Data (form data)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
            // Passwords (password)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
            // Delete All (delete all)
            //ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 2", "", ShowCommands.SW_HIDE);
            ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);


            // Method 4: Use webbrowser comes clear coookie way (not recommended, can not afford to clear session, found invalid)
            wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1));

            Method five //: Use js Clear cookie (not recommended, clearing can not afford session)
            wb.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
            // There are a, b, c, e, f;
            //f=0;
            //a=document.cookie.split('; ');
            //b='.'+'baidu.com';
            ////b='.'+'www.baidu.com';
            //for(e=0;e<a.length;e++){
            //    //b='.'+location.host;
            //    b=b.replace(/^(?:%5C.|[^%5C.]+)/,'');
            //    c=location.pathname;
            //    c=c.replace(/.$/,'');
            //    ck = a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString();
            //    console.log(ck);
            //    document.cookie=ck;
            //}

            // Method six: Use InternetSetCookie cookie assigned to a null value (not recommended)
            // This can also give Cookie empty assignment: InternetSetCookie
            //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");
Copy the code

 

method one:

Copy the code
    [System.Runtime.InteropServices.DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);


        /// <summary>
        /// InternetSetOption use in the operating wininet.dll clear webbrowser cookie
        /// </summary>
        private static unsafe void SuppressWininetBehavior()
        {
            /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
                * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
                *      A general purpose option that is used to suppress behaviors on a process-wide basis. 
                *      The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress. 
                *      This option cannot be queried with InternetQueryOption. 
                *      
                * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
                *      Suppresses the persistence of cookies, even if the server has specified them as persistent.
                *      Version:  Requires Internet Explorer 8.0 or later.
                */
            int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
            int* optionPtr = &option;

            bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
            if (!success)
            {
                MessageBox.Show("Something went wrong ! Clear Cookie Failed!");
            }

        }
Copy the code

 

Method Two:

Only one like this:

 // Method Two: Remove information after the user logs on, here led to the cancellation of the function of the browser, ie using the built-in functions (recommended)
            HtmlDocument document = wb.Document;
            document.ExecCommand("ClearAuthenticationCache", false, null);

Method three:

Copy the code
 // Method three: Delete local cookie This method will pop up a pop-up ie remove the cookie
            // Temporary Internet Files (Internet temporary files)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
            //Cookies
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
            // History (History)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
            // Form. Data (form data)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
            // Passwords (password)
            //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
            // Delete All (delete all)
            //ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 2", "", ShowCommands.SW_HIDE);
            ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);
Copy the code
ShellExecute method:
Copy the code
    public enum ShowCommands : int
        {

            SW_HIDE = 0,

            SW_SHOWNORMAL = 1,

            SW_NORMAL = 1,

            SW_SHOWMINIMIZED = 2,

            SW_SHOWMAXIMIZED = 3,

            SW_MAXIMIZE = 3,

            SW_SHOWNOACTIVATE = 4,

            SW_SHOW = 5,

            SW_MINIMIZE = 6,

            SW_SHOWMINNOACTIVE = 7,

            SW_SHOWNA = 8,

            SW_RESTORE = 9,

            SW_SHOWDEFAULT = 10,

            SW_FORCEMINIMIZE = 11,

            SW_MAX = 11

        }

        [DllImport("shell32.dll")]
        static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
Copy the code

 

Method four:

  // Method 4: Use webbrowser comes clear coookie way (not recommended, can not afford to clear session, found invalid)
            wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1));

Method five:

Copy the code
 Method five //: use js Clear cookie (not recommended clear not afford session)
            wb.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
            // There are a, b, c, e, f;
            //f=0;
            //a=document.cookie.split('; ');
            //b='.'+'baidu.com';
            ////b='.'+'www.baidu.com';
            //for(e=0;e<a.length;e++){
            //    //b='.'+location.host;
            //    b=b.replace(/^(?:%5C.|[^%5C.]+)/,'');
            //    c=location.pathname;
            //    c=c.replace(/.$/,'');
            //    ck = a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString();
            //    console.log(ck);
            //    document.cookie=ck;
            //}
Copy the code

The wb.Navigate ( "javascript: ((function () {...} void replace the contents of the code is commented below, you want to write clear cookier the domain and then you can clean up, but can not afford to clear session, this is from a foreign site appears, the actual invalid!

Method six:

 // Method six: Use InternetSetCookie cookie assigned to a null value (not recommended)
            // This can also give Cookie empty assignment: InternetSetCookie
            //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");

 

About InternetSetCookie this method own Internet search.

Guess you like

Origin www.cnblogs.com/soundcode/p/12665875.html