private void DisplayResult(AuthResult result)
        {
            if (result != null)
            {
                txtResults.SelectionColor = Color.Green;

                if (result.Type == AuthResultType.FAIL)
                    txtResults.SelectionColor = Color.Red;

                txtResults.AppendText(result.Message);
                txtResults.AppendText(Environment.NewLine);
            }
        }
        public FirewallClient(WebBrowser wb, String Username, String Password)
        {
            navigation = new TaskCompletionSource<object>();
            br = wb;
            br.DocumentCompleted += (sender, e) =>
            {
                if (br.Document != null)
                {
                    if (br.Url == e.Url)
                    {
                        if (br.Document.GetElementById("STATE") != null)
                        {

                            var step = br.Document.GetElementById("STATE").GetAttribute("value");

                            // username page
                            if (step == "1")
                            {
                                // fill in username and click submit button
                                br.Document.GetElementById("DATA").SetAttribute("value", Username);
                                br.Document.GetElementsByTagName("INPUT")[3].InvokeMember("click");
                            }
                            // password page
                            else if (step == "2")
                            {
                                // fill in password and click submit button
                                br.Document.GetElementById("DATA").OuterHtml = String.Format("<INPUT value=\"{0}\" name=DATA>", Password);
                                br.Document.GetElementsByTagName("INPUT")[3].InvokeMember("click");
                            }
                            // method page
                            else if (step == "3")
                            {
                                // click submit
                                br.Document.GetElementsByTagName("INPUT")[5].InvokeMember("click");
                            }
                        }
                        else
                        {
                            // no STATE element, so determine if valid login or invalid and requires relogin
                            AuthResult r = new AuthResult();

                            if (br.Document.Title.Contains("Authentication"))
                            {
                                // if there's an input submit button then login failed
                                var reloginButton = br.Document.GetElementsByTagName("INPUT");
                                if (reloginButton.Count > 0 && reloginButton[0].GetAttribute("value") == "Relogin")
                                {
                                    // Relogin button is present
                                    r.Message = String.Format("FAILED: {0} - Perhaps a bad username or password.", br.Url.ToString());
                                    r.Type = AuthResultType.FAIL;
                                }
                                else
                                {
                                    String numberOfRules = "0 rules";
                                    if (br.DocumentText.Contains("rules"))
                                    {
                                        numberOfRules = br.DocumentText.Split(new char[] { '(', ')' })[1];
                                    }
                                    r.Message = String.Format("SUCCESS: {0} - {1}", br.Url.ToString(), numberOfRules);
                                    r.Type = AuthResultType.PASS;
                                }

                            }
                            else
                            {
                                // Relogin button is present
                                r.Message = String.Format("FAILED: {0} - {1}.", br.Url.ToString(), br.Document.Title);
                                r.Type = AuthResultType.FAIL;
                            }

                            br.Dispose();
                            _result = r;
                        }
                    }
                }
            };
        }
        private void browser_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var br = sender as WebBrowser;

            if (br.Document != null)
            {
                if (br.Url == e.Url)
                {
                    if (br.Document.GetElementById("STATE") != null)
                    {

                        var step = br.Document.GetElementById("STATE").GetAttribute("value");

                        // username page
                        if (step == "1")
                        {
                            // fill in username and click submit button
                            br.Document.GetElementById("DATA").SetAttribute("value", txtUsername.Text);
                            br.Document.GetElementsByTagName("INPUT")[3].InvokeMember("click");
                        }
                        // password page
                        else if (step == "2")
                        {
                            // fill in password and click submit button
                            br.Document.GetElementById("DATA").OuterHtml = String.Format("<INPUT value=\"{0}\" name=DATA>", _passwordString);
                            br.Document.GetElementsByTagName("INPUT")[3].InvokeMember("click");
                        }
                        // method page
                        else if (step == "3")
                        {
                            // click submit
                            br.Document.GetElementsByTagName("INPUT")[5].InvokeMember("click");
                        }
                    }
                    else
                    {
                        // no STATE element, so determine if valid login or invalid and requires relogin
                        AuthResult r = new AuthResult();

                        if (br.Document.Title.Contains("Authentication"))
                        {
                            // if there's an input submit button then login failed
                            var reloginButton = br.Document.GetElementsByTagName("INPUT");
                            if (reloginButton.Count > 0 && reloginButton[0].GetAttribute("value") == "Relogin")
                            {
                                // Relogin button is present
                                r.Message = String.Format("FAILED: {0} - Perhaps a bad username or password.", br.Url.ToString());
                                r.Type = AuthResultType.FAIL;
                            }
                            else
                            {
                                String numberOfRules = "0 rules";
                                if (br.DocumentText.Contains("rules"))
                                {
                                    numberOfRules = br.DocumentText.Split(new char[] { '(', ')' })[1];
                                }
                                r.Message = String.Format("SUCCESS: {0} - {1}", br.Url.ToString(), numberOfRules);
                                r.Type = AuthResultType.PASS;
                            }

                        }
                        else
                        {
                            // Relogin button is present
                            r.Message = String.Format("FAILED: {0} - {1}.", br.Url.ToString(), br.Document.Title);
                            r.Type = AuthResultType.FAIL;
                        }

                        /*                
                            *  Windows Forms are not designed to work across different threads. 
                            *  Invoke method will run the delegate you pass to it in the UI thread.
                            *  If you want to manipulate UI elements via other threads, you'll have to run the actual manipulation on the UI thread. 
                            *  A control's InvokeRequired property will also tell you if you need to use Invoke rather than calling the method directly.
                           */
                        Invoke(new Action(() => DisplayResult(r)));
                        br.Dispose();
                    }
                }
            }
        }