Basic authentication in the webBrowser control

19 August 2006
For a small project I had to display web pages within a Windows form, no problem, just drag a webBrowser control onto the form set a url. Works fine but it turned out the pages I have to display require basic authentication over SSL. There is no property on the control to set the credentials like there is for the .net WebRequests. There are some people on the net asking about this but I didn't find an easy answer. I went through all the members of the control and in addition to the url property I used before I found the Navigate method, one of the overloads takes an AdditionalHeader parameter. I couldn't exactly remember how the authentication header looked like, so I fired up Wfetch (IIS Resource Kit) to look at them. The header in question is something like:

Authorization: Basic RmlsZVVwbG9hZXVyOjG4NUgkOTNT7UpTeQM=\r\n

the crypted string in the middle is a base64 version of "myusername: mypassword"
the whole code is:

    
     System.Uri uri = new Uri("http://www.mysite.xxx");

     // encode the credentials in Base64
     byte[] authData = System.Text.UnicodeEncoding.UTF8.GetBytes("myusername: mypassword");
            
     // build the whole header
     string authHeader = "Authorization: Basic " + Convert.ToBase64String(authData) + "\r\n";

     // open the page with the extra header
     webBrowser1.Navigate(uri, "", null, authHeader);

Pages in this section

Categories

ASP.Net | Community | Development | IIS | IT Pro | Security | SQL (Server) | Tools | Web | Work on the road | Windows