WebBrowser control - how to reset cookies/cache RRS feed

  • Soru

  • Hello,

    I am working on an application that contains a function to check website credentials by opening the website in WebBrowser and filing in username and password fields. The user then just pres login to validate it. This works great.

    The problem is that this could by done only once after running the application, because WebBrowser keeps that website in login state and unfortunatelly there is no option to log- out, only time-out.

    When I close and reopen the application, webbrowser does not remember the last login and I can check another one credentials.

    I want to by able to test multiple credentials without having to close and reopent the application.

    How Can I reset the WebBrowser or delete it cookies or cache?

    Thank you

    29 Mayıs 2015 Cuma 13:23

Tüm Yanıtlar

  • Try this:

            WebBrowser1.Document.Cookie.Remove(0, WebBrowser1.Document.Cookie.Length)

    Fouad Roumieh

    29 Mayıs 2015 Cuma 16:44
  • thanks for the reply, but not working.

    it is still keeping user signed in

    29 Mayıs 2015 Cuma 18:04
  • Well this looks pretty ugly, but it worked for me (using Javascript):

    WebBrowser1.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());}}}})())")

    Paul ~~~~ Microsoft MVP (Visual Basic)

    29 Mayıs 2015 Cuma 19:03
  • still not working, tested on two sites now. 

    Isn't there a way to programatically restart the application? It is OK  when td is done manually 

    29 Mayıs 2015 Cuma 20:39
  • I tested against a site that implements Forms Authentication. What type of authentication are the web sites you are testing with configured for?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    29 Mayıs 2015 Cuma 21:44
  • thanks for the reply, but not working.

    it is still keeping user signed in

    How did you implement it? Are you navigating again to the login page after delete?

    Fouad Roumieh

    30 Mayıs 2015 Cumartesi 04:31
  • still not working, tested on two sites now. 

    Isn't there a way to programatically restart the application? It is OK  when td is done manually 

    I logged into this forums site. Then brought up another instance of I.E. at googles homepage. Exited out of the instance of I.E. at this site. In the only instance of I.E. open, at googles homepage, I deleted everything in the image below. Then went back to this site by selecting if from favorites in the open instance of I.E. and was still logged on.

    I believe this code in sequence will cause your application to launch a new instance of it while making the current instance exit. Although I didn't try it. And it's possible that if the app is a "single instance" app that this code may not work since process.start is called prior to this instance exiting.

    Application.ExecutablePath Property


    La vida loca

    30 Mayıs 2015 Cumartesi 05:21
  • This code instantiates a WebBrowser object and disposes of it when done.

    I tested the code by selecting Button1 with the Forums home URL, https://social.msdn.microsoft.com/Forums/en-US/home, in TextBox1. Then I logged on and looked at some stuff. Then I selected Button1 again with the URL still in TextBox1 which took me from the Forum page I was looking at back to the homepage. Because the first WebBrowser object was disposed and a new WebBrowser object navigated to the homepage I was no longer logged on when the homepage displayed in the second WebBrowser object.

    1st image is prior to selecting Button1 therefore no WebBrowser is displayed and no URL is yet in the TextBox. 2nd image is prior to logging on.

    Option Strict On
    Imports System.Windows.Forms
    Public Class Form1
        Dim AWebBrowser As New List(Of WebBrowser)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If AWebBrowser.Count > 0 Then
                For i = AWebBrowser.Count - 1 To 0 Step -1
                        RemoveHandler AWebBrowser(i).DocumentCompleted, AddressOf WebBrowser1_DocumentCompleted
                    Catch ex As Exception
                    End Try
            End If
            AWebBrowser.Add(New WebBrowser)
            With AWebBrowser(0)
                .Left = 20
                .Top = 50
                .Width = Me.ClientRectangle.Width - 40
                .Height = Me.ClientRectangle.Height - 70
                .Anchor = CType(AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Right, AnchorStyles)
            End With
            AddHandler AWebBrowser(0).DocumentCompleted, AddressOf WebBrowser1_DocumentCompleted
            If TextBox1.Text <> "" Then
                Catch ex As Exception
                End Try
            End If
        End Sub
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
            MessageBox.Show("Seems to be working.")
        End Sub
    End Class

    La vida loca

    30 Mayıs 2015 Cumartesi 06:02
  • You never know what kind of logon mechanism a website uses.

    Maybe it simply uses your IP address, then you cannot do anything against it. 

    Therefore try this first with IE.


    30 Mayıs 2015 Cumartesi 08:04
  • (MCC) >


    not working, still logged in. Tried at 2 websites + https://social.msdn.microsoft.com/Forums/en-US/home

    (I tried it also in new project)

    Relaunching the app helps, but it is not much convinient

    2 Haziran 2015 Salı 14:39
  • Fouad Roumieh> I log in > navigate away(google...) > press button to delete cookies > navigate to the site and I am still logged in
    2 Haziran 2015 Salı 14:47
  • Its simple. Just enable the webbrowser context menu. Then run the application and right click on the webbrowser, there you will find an option for deleting cookies as well saving to bookmarks.
    28 Eylül 2020 Pazartesi 15:45