protected virtual void UpdateWebRequest(SharePointConnection connection, WebRequest request)
        {
            if (connection.RequestTimeout != null)
            {
                request.Timeout = connection.RequestTimeout.Value;
            }

            var httpRequest = request as HttpWebRequest;

            if (httpRequest == null)
            {
                return;
            }

            if (IsOnline(connection))
            {
                UpdateHttpWebRequestForOnline(connection, httpRequest);
            }
            else
            {
                httpRequest.Credentials = connection.Credentials;

                // if this context is using default Windows authentication add a WebRequest Header to stop forms auth from potentially interfering.
                if (connection.AuthenticationMode.GetValueOrDefault() == ClientAuthenticationMode.Default)
                {
                    httpRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
                }
            }
        }
 protected virtual void UpdateClientContextForOnline(SharePointConnection connection, ClientContext context)
 {
     context.ExecutingWebRequest += (sender, args) =>
     {
         args.WebRequestExecutor.WebRequest.CookieContainer = CreateCookies(connection);
     };
 }
        /// <summary>
        /// Creates a web request based on connection settings.
        /// </summary>
        public virtual WebRequest CreateHttpWebRequest(SharePointConnection connection, Uri uri)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            var request = WebRequest.Create(uri);

            UpdateWebRequest(connection, request);

            return(request);
        }
        /// <summary>
        /// Creates a web client based on connection settings.
        /// </summary>
        public virtual WebClient CreateWebClient(SharePointConnection connection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            var client = new SharePointWebClient(request => UpdateWebRequest(connection, request))
            {
                BaseAddress = connection.Url.OriginalString + (!connection.Url.OriginalString.EndsWith("/") ? "/" : string.Empty)
            };

            return(client);
        }
        private static CookieContainer CreateCookieContainer(SharePointConnection connection)
        {
            if (connection.Credentials == null)
            {
                throw new NullReferenceException("The Credentials property of the connection is required.");
            }

            var authType    = connection.AuthenticationMode != null ? connection.AuthenticationMode.Value : ClientAuthenticationMode.Default;
            var credentials = connection.Credentials.GetCredential(connection.Url, authType.ToString());

            var container = CreateCookieContainer(connection.Url, credentials.UserName, credentials.Password, connection.RequestTimeout);

            return(container);
        }
        /// <summary>
        /// Creates a context based on connection settings.
        /// </summary>
        public virtual ClientContext CreateClientContext(SharePointConnection connection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            var context = connection.CreateClientContext();

            if (IsOnline(connection))
            {
                UpdateClientContextForOnline(connection, context);
            }

            return(context);
        }
        protected virtual CookieContainer CreateCookies(SharePointConnection connection)
        {
            var expiryWindow = connection.CookieExpiryWindow;

            if (expiryWindow != TimeSpan.Zero)
            {
                CookieContainer container;
                var             key = connection.GetConnectionId();

                if (!_cookiesLookup.TryGetValue(key, out container) || CheckIfAnyCookieIsExpired(container, connection.Url, expiryWindow))
                {
                    container           = CreateCookieContainer(connection);
                    _cookiesLookup[key] = container;
                }

                return(container);
            }

            var cookies = CreateCookieContainer(connection);

            return(cookies);
        }
 protected virtual void UpdateHttpWebRequestForOnline(SharePointConnection connection, HttpWebRequest request)
 {
     request.CookieContainer = CreateCookies(connection);
 }
 protected virtual bool IsOnline(SharePointConnection connection)
 {
     return(GetOnlineDomainsSetting().Any(domain => connection.Url.Host.EndsWith(domain, StringComparison.OrdinalIgnoreCase)));
 }