Implement custom 401 handling for a WebBrowser control

As per this article, I've extended the System.Windows.Forms.WebBrowser class to implement custom error-handling. Mostly, it works.

The problem comes when the browser gets a "401 Unauthorized" response. That kind of response causes the WebBrowser control to display the standard Username / Password dialog. The NavigateError event isn't fired until that dialog is cancelled.

So what can I do to capture the 401 response and handle it in my own custom way?

I assumed there would be something I could do, such as that which I do to capture the NavigateError event, and handle those my own way but I haven't seen anything.

Edit: Solution Found!
The important steps are:
1. The WebBrowser control must first be navigated to a non-secure page ("about:blank" is the typical URL used) in order to avoid KB 320153
2. The host for the WebBrowser control must implement IOleClientSite, IServiceProvider, and IAuthenticate.
3. IServiceProvider.QueryService must handle the IAuthenticate service request with the IAuthenticate implementation, all other service requests can be handled with the INET_E_DEFAULT_ACTION response.
4. IAuthenticate.Authenticate is your custom authentication handler.

-------------Problems Reply------------

implement IAuthenticate and IAuthenticateEx on your webbrowser host. Basically, your IOleClientSite implementation needs to responde IServiceProvider.QueryService, and return an IAuthenticate(Ex) interface (not the managed one, the native one returned from Marshal.GetComInterfaceForObject) when the service is IID_IAuthenticate. For unrecognized service requests, QueryService should return INET_E_DEFAULT_ACTION.

I don't think the WPF webbrowser has extension points for its IOleClientSite implementation. You can try host a Winform webbrowser class which has an overriden CreateWebBrowserSiteBase virtual method that provides the IAuthenticate(Ex) implementation, or write a webbrowser wrapper from the ground up.

This may not work in a Citrix session.

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:[email protected] 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;
var newPage = BaseUrl + e.Uri.AbsolutePath;

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

Category:c# Views:0 Time:2011-11-14

Related post

  • How to Handle Threading for WebBrowser Control 2010-11-05

    I am using BrowserControl to navigate to range of web pages on a site and then parse the html and extract information about books etc… I am having problems related (I think) to threading… I have something like this. // MAIN LOOP for (int i = 0; i

  • Custom Paint handler on a WinForms Control inside a WPF application 2012-02-08

    I have a WPF application with a Windows Form element hosted inside it, using this method: System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost(); gMapZoom = new GMap(); gMapZoom.Paint += new P

  • Implementing custom "path" properties inside a User Control 2012-02-16

    As you might noticed, ComboBox, ListBox and that kind of "selectors" controls uses some properties with the suffix "path" which allows the binding of objects with different property names to its datatemplate without changing the template itself. Cert

  • Security Level for WebBrowser control 2010-04-05

    I am trying to migrate an .hta application to a C# executable. Of course, since it's an .hta the code is all HTML and Jscript, with calls to local ActiveX objects. I created a C# executable project and am just using the WebBrowser control to display

  • .net webbrowser control 2008-10-22

    I am using the webbrowser control in visual studio. I think it is a wrapper around internet explorer. Anyway all is going well I am using it in edit mode however I can't get he document's keydown event to fire (in order to catch ctrl+v) anyone had si

  • How to carry out Cross Domain request in a Webbrowser Control? 2009-01-06

    As you know doing Cross Domain XMLHTTP requests is not allowed for security reasons under Internet Explorer. I have a WebBrowser Control and I'm using DocumentText instead of Navigate to a URL. Since the current domain is about:blank when the page tr

  • ASP.Net MVC Custom Error handling via Action Filter Attributes 2009-12-30

    I am trying to implement Custom Error handling via Action Filter Attributes. My code is as follows: [HandleError (Order = 2)] [HandleError (Order = 1, ExceptionType = typeof(NullReferenceException), View = "CustomError")] public class ArticlesControl

  • Custom session handler in php using db 2012-01-16

    I am having problem with implementing custom session handler in php. The code: I initialize this once in my frameworks bootstrap, after I have connection with db: require_once 'DbSession.php'; $session = new DbSession();

  • handling Security Messages in .NET WebBrowser Control 2010-02-19

    I am attempting to write a application which will allow users to create their own widget plugins in html/jscript and i would like to set my own security options in the applications settings allowing users to choose what can run or not eg scrip

  • How to create a custom dialog with embedded WebBrowser control for web authentication in installer using WiX 2012-04-12

    I'm looking for a way to embed the webbrowser control to installer to perform web based authentication. Is there any way to implement it? Implementing simple dialog with webcontrol will be a good starting point for me. --------------Solutions--------

  • Handling key events on WebBrowser control 2009-03-16

    I am using the WebBrowser control in a C# application and want to handle all key events while the WebBrowser has the focus, regardless what individual content element (input field, link, etc.) is focused. I tried to simply add an event handler to bro

  • How can I get a handle or object reference to an HtmlElement from a WebBrowser control HtmlDocument? 2010-10-27

    Think "Firebug", but entirely from C#. I have a WebBrowser control that I've built a DOM tree for in a TreeView. I'd like to be able to set a link between each DOM element in the TreeView and its matching HtmlElement in the WebBrowser's Document so t

  • Implementing custom configuration section handler 2010-11-02

    gleanings from a variety of sources (including stackOverlflow), however when I come to use it, I get the following error message "The Configuration property 'deviceconfig' may not be derived from ConfigurationSection." I have been struggling with thi

  • Handling hardware back button and sending it to WebBrowser control running on Windows Phone 2011-02-24

    I have a web browser control embedded into a PhoneApplicationPage. I have to handle the hardware back button and force the web browser to go back. I know how to handle hardware back button. How do you force the webbrowser to go to a previous page? Th

  • How to handle javascript events via WebBrowser control for WinForms 2011-03-02

    I have read WebBrowser Control from .Net — How to Inject Javascript, Is it possible to call Javascript method from c# winforms and many others. Those examples were returns function value or alert window (synchronous calls). I have to get result from

  • WebBrowser control that can handle HTTPS 2011-04-08

    I am searching for a substitute of the WebBrowser control in .NET which can handle HTTPS. This question is similar here on Stack Overflow, but none of these controls offer HTTPS. btw, just to mention, I tried all these GeckoFx - development stopped S

  • Why does invalid handle exception occur when browsing via WPF WebBrowser control? 2011-05-05

    I have a WPF app that has a window with a WebBrowser control in it. I navigate to a site like, it loads, and then I get an error when I click a link. This only happens when debugging on a Windows Server VM that has VS 2010 on it. R

  • Implement IDispatch::Invoke to be called by a WebBrowser control 2011-09-30

    I'm trying to do what they explain on this article in the Controlling Download and Execution section. I understand the Web Browser Control in that context is not .NET's WebBrowser. What I'm trying to do is to gain control over what the WebBrowser con

  • Is there a way to hijack the keyboard shortcuts that are handled by the System.Windows.Forms.WebBrowser control? 2012-01-19

    I have a WebBrowser control that is used to generate formatted HTML code in our application. However, keyboard shortcuts like Copy/Cut/Paste/SelectAll are handed by the control when WebBrowserShortcutsEnabled is set to true. These operations are fine

Copyright (C), All Rights Reserved.

processed in 0.100 (s). 11 q(s)