开发者

.NET Webbrowser control with additional headers

开发者 https://www.devze.com 2022-12-14 01:08 出处:网络
I am using a webbrowser control and loading a website that uses basic authentication.I pass the username and password in the additional header of the navigate routine.The problem is that the header is

I am using a webbrowser control and loading a website that uses basic authentication. I pass the username and password in the additional header of the navigate routine. The problem is that the header is not retained while navigating the site.

I.E. I log into the site and pass the user name and password in the http header. When I click a link to access another page the header is removed and the site asks for credentials. If I was to just type the site into IE and log in the first time the headers are carried forward on all pages.

Anybody have any ideas how I can have the page retain the headers so the user never has to authenticate?

Dim sHeaders As String
Dim HelpBrowser As New WebBrowser

sHeaders = "Authorization: Bas开发者_运维百科ic: " & System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("UserName:Password")) & Chr(13) & Chr(10)

Me.HelpBrowser.Navigate("http://www.mysite.com", True, Nothing, sHeaders)


Suppose the user name and password for authentication is user1 and pass1. Use the following statement to provide basic auth information that will not be lost after redirection:

Me.HelpBrowser.Navigate("http://user1:pass1@www.mysite.com")


Passing credentials in the URL has been disabled by default in IE per KB834489.

A registry change must be made to allow this kind of navigation:

To disable the new default behavior in Windows Explorer and Internet Explorer, create iexplorer.exe and explorer.exe DWORD values in one of the following registry keys and set their value data to 0.

•For all users of the program, set the value in the following registry key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE

•For the current user of the program only, set the value in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE

For more information, see: http://support.microsoft.com/default.aspx?scid=kb;[LN];834489


The following is in c#, but I would hope there is a similar Navigating event for vb. I found that to be able to navigate the site without the Authorization Header getting lost or removed I had to do the following otherwise for each new page the user was prompted again. This solution also does not require the user:password@site syntax to be enabled.

    private bool _redirected = false;
    private const string BaseUrl = @"http://mySite";

    private void Navigate()
    {
        var helpUrl = BaseUrl;
        var authHeader = GetAuthHeader();

        _docWindow.Browser.Navigate(helpUrl, string.Empty, null, authHeader);           
        _docWindow.Browser.Navigating += Browser_Navigating;

    }

    private string GetAuthHeader()
    {
        byte[] authData = UnicodeEncoding.UTF8.GetBytes(_userName + ":" + _password);
        string authHeader = "Authorization: Basic " + Convert.ToBase64String(authData);
        return authHeader;
    }

    void Browser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
    {            
        if (_redirected)
        {
            _redirected = false;
            return;
        }
        var newPage = BaseUrl + e.Uri.AbsolutePath;

        e.Cancel = true;
        _redirected = true;
        _docWindow.Browser.Navigate(newPage, string.Empty, null, GetAuthHeader());
    }
0

精彩评论

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