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.