Anthony
Anthony

Reputation: 165

How can I force web browser control to remember proxy credentials setting?

I'm using .net web browser control to open an url in new window through proxy with credentials. I use this code for that:

Public Function AppendHeader(ByRef OriginalHeader As String, ByVal Addition As String) As Boolean
    If OriginalHeader <> "" Then
        OriginalHeader = OriginalHeader + vbNewLine
    End If
    OriginalHeader = OriginalHeader + Addition
    OriginalHeader.Trim(vbNewLine.ToCharArray)
End Function

Public Function Base64Enc(ByRef s As String) As String
    Base64Enc = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(s))
End Function

Public Sub Navigate()
    Dim webBrowser As WebBrowser = New WebBrowser()
    Dim headers As String = ""
    AppendHeader(headers, "Proxy-Authorization: Basic " & Base64Enc("user:pass"))
    'AppendHeader(headers, "Authorization: Basic " & Base64Enc("user:pass"))
    webBrowser.Navigate("http://stackoverflow.com", Guid.NewGuid().ToString(), Nothing, headers)
End Sub

This code helps to hide Windows Security window at the first time, but if loading web page sends requests to other urls this window shows again and again (you can see it on a screenshot below).

screenshot

So what can I do to solve this problem? (I'm using winforms and vb.net, but C# is suitable too)

Upvotes: 2

Views: 1172

Answers (1)

EJD
EJD

Reputation: 751

Give this a try I am unable to test it :)

Private Sub webBrowser_Navigating(ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs)

    Dim credentials As New System.Net.NetworkCredential("user", "pwd", "MyDomain") 
    Dim proxy As New System.Net.WebProxy("127.0.1.2", 80) 
    If proxy Is Nothing Then
        e.Cancel = True
    End If

End Sub

Upvotes: 1

Related Questions