BetterCMS.Module.LuceneSearch.Services.WebCrawlerService.DefaultWebCrawlerService.TryAuthenticate C# (CSharp) Метод

TryAuthenticate() приватный Метод

private TryAuthenticate ( ) : bool
Результат bool
        private bool TryAuthenticate()
        {
            var url = cmsConfiguration.Search.GetValue(LuceneSearchConstants.ConfigurationKeys.LuceneAuthorizationUrl);
            if (string.IsNullOrWhiteSpace(url))
            {
                authorizationCookies = new CookieCollection();
                Log.ErrorFormat("Lucene web crawler: failed to authenticate user: url is not set in lucene configuration.");

                return false;
            }

            authorizationCookies = null;
            string parameters = "";
            var prefixLength = LuceneSearchConstants.ConfigurationKeys.LuceneAuthorizationFormFieldPrefix.Length;
            foreach (KeyValueElement element in (ConfigurationElementCollection)cmsConfiguration.Search)
            {
                if (!element.Key.StartsWith(LuceneSearchConstants.ConfigurationKeys.LuceneAuthorizationFormFieldPrefix, StringComparison.Ordinal))
                {
                    continue;
                }
                if (!string.IsNullOrWhiteSpace(parameters))
                {
                    parameters = string.Concat(parameters, "&");
                }
                parameters = string.Format("{0}{1}={2}", parameters, element.Key.Substring(prefixLength, element.Key.Length - prefixLength), HttpUtility.UrlEncode(element.Value));
            }

            var requestLogin = (HttpWebRequest)WebRequest.Create(url);
            requestLogin.Method = "POST";
            requestLogin.CookieContainer = new CookieContainer();
            requestLogin.ContentType = "application/x-www-form-urlencoded";
            requestLogin.AllowAutoRedirect = false;
            requestLogin.ContentLength = parameters.Length;

            HttpWebResponse httpWebResponse = null;
            try
            {
                using (StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), Encoding.ASCII))
                {
                    stOut.Write(parameters);
                    stOut.Close();
                }

                httpWebResponse = (HttpWebResponse)requestLogin.GetResponse();
                var cookies = httpWebResponse.Cookies;
                authorizationCookies = cookies;

                foreach (Cookie cookie in authorizationCookies)
                {
                    cookie.Expires = DateTime.Now.AddYears(1);
                }
            }
            catch (Exception exc)
            {
                Log.ErrorFormat("Lucene web crawler: Failed to authenticate user.", exc);

                return false;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
            }

            return true;
        }
    }