Esempio n. 1
0
        private SP.ClientContext GetClientContext(string webUrl)
        {
            SP.ClientContext ctx = new SP.ClientContext(webUrl);
            if (txtUser.Text != "")
            {
                string domain = "";
                string user   = "";
                if (txtUser.Text.Replace('/', '\\').Contains('\\'))
                {
                    string[] tmp = txtUser.Text.Split('\\');
                    domain = tmp[0];
                    user   = tmp[1];
                }
                else
                {
                    user = txtUser.Text;
                }
                string password = txtPwd.Text;

                if (chkLoginOnline.Checked)
                {
                    System.Security.SecureString secpwd = new System.Security.SecureString();
                    for (int i = 0; i < password.Length; i++)
                    {
                        secpwd.AppendChar(password[i]);
                    }
                    //ClientAuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
                    //FormsAuthenticationLoginInfo creds = new FormsAuthenticationLoginInfo(User, Password);
                    SharePointOnlineCredentials creds = new SharePointOnlineCredentials(user, secpwd);
                    ctx.Credentials = creds;
                }
                else
                {
                    ctx.Credentials = new System.Net.NetworkCredential(user, txtPwd.Text, domain);
                }
            }
            if (string.IsNullOrEmpty(ProxyUrl) == false)
            {
                ctx.ExecutingWebRequest += (sen, args) =>
                {
                    System.Net.WebProxy myProxy = new System.Net.WebProxy(ProxyUrl);
                    string domain = "";
                    string user   = "";
                    if (ProxyUser.Replace('/', '\\').Contains('\\'))
                    {
                        string[] tmp = ProxyUser.Split('\\');
                        domain = tmp[0];
                        user   = tmp[1];
                    }
                    // myProxy.Credentials = new System.Net.NetworkCredential(ProxyUser, ProxyPwd, domain);
                    myProxy.Credentials                      = System.Net.CredentialCache.DefaultCredentials;
                    myProxy.UseDefaultCredentials            = true;
                    args.WebRequestExecutor.WebRequest.Proxy = myProxy;
                };
            }

            return(ctx);
        }
Esempio n. 2
0
        /// <summary>
        ///     Sends all the messages
        /// </summary>
        /// <param name="message"></param>
        /// <returns>
        ///     <value>true</value>
        ///     if succeeded
        /// </returns>
        private bool SendFast(string message)
        {
            _conversationActiveFlag.Wait(_terminateProcessor.Token);
            try
            {
                var http = (HttpWebRequest)WebRequest.Create(Url);
                http.KeepAlive = false;
                http.Method    = Method;

                http.ContentType = ContentType;
                http.Accept      = Accept;
                http.Timeout     = ConnectTimeout;
                http.Proxy       = String.IsNullOrWhiteSpace(ProxyUrl) ? NoProxy :
                                   new WebProxy(new Uri(ProxyUrl))
                {
                    UseDefaultCredentials = String.IsNullOrWhiteSpace(ProxyUser)
                };
                if (!String.IsNullOrWhiteSpace(ProxyUser))
                {
                    var cred = ProxyUser.Split('\\');
                    http.Proxy.Credentials = cred.Length == 1
                        ? new NetworkCredential {
                        UserName = ProxyUser, Password = ProxyPassword
                    }
                        : new NetworkCredential {
                        Domain = cred[0], UserName = cred[1], Password = ProxyPassword
                    };
                }

                if (IgnoreSslErrors)
                {
                    http.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
                }
                if (!string.IsNullOrWhiteSpace(Authorization))
                {
                    http.Headers.Add("Authorization", Authorization);
                }

                var bytes = Encoding.ASCII.GetBytes(message);
                http.ContentLength = bytes.Length;
                using (var os = http.GetRequestStream())
                {
                    os.Write(bytes, 0, bytes.Length); //Push it out there
                }

                using (var response = http.GetResponseAsync())
                {
                    using (var sr =
                               new StreamReader(response.Result.GetResponseStream() ?? throw new InvalidOperationException()))
                    {
                        //TODO What should we check for>
                        // ReSharper disable once UnusedVariable
                        var content = sr.ReadToEnd();
                    }

                    return(!response.IsFaulted);
                }
            }
            catch (WebException wex)
            {
                InternalLogger.Warn(wex, "Failed to communicate over HTTP");
                return(false);
            }
            catch (Exception ex)
            {
                InternalLogger.Warn(ex, "Unknown exception occured");
                return(false);
            }
            finally
            {
                _conversationActiveFlag.Release();
            }
        }