开发者

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.GetByt开发者_StackOverflow中文版es("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?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜