Search code examples
authenticationauthorizationdotnetbrowser

How to handle authorization with DotNetBrowser user control?


I could't figure out loading urls that requires authentication with DotNetBrowser control.

IE and Chrome browsers display a dialog and asks for a user name and a password.
But DotNetBrowser displays the text below :

HTTP Error 401 - Unauthorized: Access is denied

How can I make DotNetBrowser show a login dialog?


Solution

  • The following article explains how to handle HTTP authentication in DotNetBrowser:

    https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110052-handling-basic-digest-and-ntlm-authentication

    In two words, it is necessary to implement and register a custom implementation of the NetworkDelegate interface in order to provide user name and password when necessary. For example:

    public class CustomNetworkDelegate : DefaultNetworkDelegate 
    {
        public bool OnAuthRequired(AuthRequiredParams parameters)
        {
             if (!parameters.IsProxy) {
                parameters.Username = "proxy-username";
                parameters.Password = "proxy-password";
                // Don't cancel authentication
                return false;
            }
        // Cancel authentication
        return true;
        }
    }
    

    This implementation is then registered as shown below:

    browserView.Browser.Context.NetworkService.NetworkDelegate = new CustomNetworkDelegate();
    

    It is possible to show the dialog or to handle authentication in the application code directly. You can also register the existing implementations (WinFormsDefaultNetworkDelegate or WPFDefaultNetworkDelegate) that contain ready-to-use authentication dialogs.