Exemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns>empty string when ok, err message when an error was raised</returns>
        public string LogOnToBugzilla( string username, string password)
        {
            HttpWebResponse respLogin = null;
            HttpHelper httpDialog = null;

            #if DEBUG
            Stopwatch watch = Stopwatch.StartNew();
            #endif
            MyLogger.Write("Starting LogOn!", "LogOnToBugzilla", LoggingCategory.General);

            try
            {
                TDSettings.ConnectionRow connection = MyZillaSettingsDataSet.GetInstance().GetConnectionById(_connectionId);

                string myZillaUrl = connection.URL;

                string url = String.Concat(myZillaUrl, INDEX_PAGE);

                httpDialog = new HttpHelper(_connectionId, connection.Charset);

                httpDialog.BugzillaCharset = connection.Charset;

                string dataToPost = httpDialog.BuildBugzillaLogOnData(username, password);

                // POST
                respLogin = httpDialog.PostToUrlWhenLogOn(url, dataToPost, false);

            #if DEBUG
                watch.Stop();
                MyLogger.Write("LogOn took: " + watch.ElapsedMilliseconds, "LogOnToBugzilla", LoggingCategory.Debug);
            #endif

                if (respLogin != null && respLogin.Cookies != null && respLogin.Cookies.Count == 2)
                {
                    // user is logged.

                    CookieManager.AddNewCookieContainer(_connectionId, respLogin.Cookies);

                    return String.Empty;
                }
                else
                {
                    // log failed.
                    if (respLogin != null && respLogin.Cookies != null)
                    {
                        MyLogger.Write(String.Format("LogOn failed! Cookies.Count: {0}", respLogin.Cookies.Count), "LogOnToBugzilla", LoggingCategory.Warning);
                    }
                    else if (respLogin.Cookies == null) {
                        MyLogger.Write("LogOn failed! Cookie collection is NULL", "LogOnToBugzilla", LoggingCategory.Warning);
                    }

                    CookieManager.DeleteCookiesForUser(_connectionId);

                    return "Invalid user or password";
                }
            }
            catch (Exception ex)
            {
                if (httpDialog != null)
                {
                    CookieManager.DeleteCookiesForUser(_connectionId);
                }

                return ex.Message;
            }
            finally
            {
                if (respLogin != null)
                {
                    respLogin.Close();
                }

                MyLogger.Write("LogOn completed!", "LogOnToBugzilla", LoggingCategory.General);
            }
        }