7

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?

NLV
  • 21,141
  • 40
  • 118
  • 183

1 Answers1

4

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.

Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • That was just a hunch. Can I get 401 generated because I don't have the user agent specified? What are the things that are mandatory in a http header? – NLV Jun 09 '11 at 13:51
  • @NLV: I am not sure what is required in your case. You say that navigation from IE works? Check the headers in the request made from IE. – Edwin de Koning Jun 09 '11 at 14:02
  • I tried the same code in a windows forms application. Even if I explicitly set the authentication header to be empty and request the page I'm able to load the page in the control. And the web app is not configured for anonymous access. Can you explain that? I start thinking that whatever valid auth headers I'm setting to the request it is not responding. – NLV Jun 10 '11 at 10:26