Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting authentication header for WebBrowser control - ASP.NET

I'm using WebBrowser control in my asp.net application to generate screenshots of web pages. I was able to generate images till I changed the application pool account. I've given the new application pool all the necessary rights and I'm able to browse my pages from IE. But when I navigate to it through the web browser control programatically I'm getting 401.2. I've setup "Failed request tracing rules" in my IIS 7.0 and have used Fiddler to sniff the requests and I've found that the authentication header is not right.

But the webbrowser.navigate has an overloaded method where we can pass custom http headers. How can I set it? I just want to use the integrated authentication and don't want to give the username/password in the code because I might not know it. But still I tried the following code with no luck.

System.Uri uri = new Uri(myUrl);
byte[] authData = System.Text.UnicodeEncoding.UTF8.GetBytes("user: passwd");
string authHeader = "Authorization: Basic " + Convert.ToBase64String(authData) + "\r\n";
myBrowser.Navigate(uri, "", null, authHeader);

Any ideas how to set the header with other details such as useragent too?

like image 309
NLV Avatar asked Jun 09 '11 13:06

NLV


1 Answers

If you want to include the user agent in the header, you should probably do something like this:

string authHeader = "Authorization: Basic " + Convert.ToBase64String(authData) + 
        "\r\n" + "User-Agent: MyUserAgent\r\n";

Just remember that each header must be terminated with a carriage return linefeed pair.

like image 173
Edwin de Koning Avatar answered Oct 05 '22 13:10

Edwin de Koning