/// <summary>
        /// This method will return a ClientContext object with the authentication cookie set.
        /// The ClientContext should be disposed of as any other IDisposable
        /// </summary>
        /// <param name="targetSiteUrl"></param>
        /// <returns></returns>
        public static ClientContext GetAuthenticatedContext(string targetSiteUrl, int popUpWidth, int popUpHeight, out bool isCancelled)
        {
            CookieCollection cookies = null;

            cookies = ClaimClientContext.GetAuthenticatedCookies(targetSiteUrl, popUpWidth, popUpHeight, out isCancelled);
            if (cookies == null)
            {
                return(null);
            }

            ClientContext context = new ClientContext(targetSiteUrl);

            try
            {
                context.ExecutingWebRequest += delegate(object sender, WebRequestEventArgs e)
                {
                    e.WebRequestExecutor.WebRequest.CookieContainer = new CookieContainer();
                    foreach (Cookie cookie in cookies)
                    {
                        e.WebRequestExecutor.WebRequest.CookieContainer.Add(cookie);
                    }
                };
            }
            catch
            {
                if (context != null)
                {
                    context.Dispose();
                }
                throw;
            }

            return(context);
        }
Example #2
0
        /// <summary>
        /// Initializes the ClientContext.
        /// </summary>
        public void InitClientContext()
        {
            try
            {
                LogUtil.LogMessage("Initializing site collection (ClientContext) for {0}.", this.Url);

                // Set client context
                this.ClientContext = new SPClient.ClientContext(this.Url);
                this.ClientContext.ApplicationName = ProductUtil.GetProductName();

                // Set authentication mode and credentials
                switch (this.Authentication)
                {
                case AuthenticationMode.Default:
                    this.ClientContext.AuthenticationMode = ClientAuthenticationMode.Default;
                    if (this.UseCurrentCredentials)
                    {
                        LogUtil.LogMessage("Using current user credentials for user '{0}\\{1}'.", Environment.UserDomainName, Environment.UserName);
                        this.ClientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
                    }
                    else
                    {
                        LogUtil.LogMessage("Using custom credentials for user '{0}'.", this.UserName);
                        this.ClientContext.Credentials = new NetworkCredential(this.UserName, this.Password);
                    }
                    break;

                case AuthenticationMode.SharePointOnline:
                    LogUtil.LogMessage("Using SharePoint Online credentials for user '{0}'.", this.UserName);
                    this.ClientContext.AuthenticationMode = ClientAuthenticationMode.Default;
                    this.ClientContext.Credentials        = new SharePointOnlineCredentials(this.UserName, this.Password.GetSecureString());
                    break;

                case AuthenticationMode.Anonymous:
                    LogUtil.LogMessage("Using anonymous access.");
                    this.ClientContext.AuthenticationMode = ClientAuthenticationMode.Anonymous;
                    break;

                case AuthenticationMode.Forms:
                    LogUtil.LogMessage("Using Forms Based authentication for user '{0}'.", this.UserName);
                    this.ClientContext.AuthenticationMode           = ClientAuthenticationMode.FormsAuthentication;
                    this.ClientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(this.UserName, this.Password);
                    break;

                case AuthenticationMode.Claims:
                    LogUtil.LogMessage("Using Claims authentication.");
                    bool isCancelled = false;
                    this.ClientContext = ClaimClientContext.GetAuthenticatedContext(this.Url.ToString(), out isCancelled);
                    if (isCancelled)
                    {
                        throw new Exception("Could not load site. Please retry.", new Exception("Loading site cancelled by user."));
                    }
                    break;

                default:
                    throw new NotImplementedException("Current authentication mode not supported.");
                }

                LogUtil.LogMessage("Valide (execute) the ClientContext.");

                // Try connection, to ensure site is available
                SPClient.Site site = this.ClientContext.Site;
                this.ClientContext.Load(site);
                this.ClientContext.ExecuteQuery();

                // After succes set variables
                this.IsLoaded = true;
                this.LoadDate = DateTime.Now;

                LogUtil.LogMessage("ClientContext successful loaded.");
                LogUtil.LogMessage("ClientContext technical data. ServerVersion: {0}. ServerSchemaVersion: {1}. ServerLibraryVersion: {2}. RequestSchemaVersion: {3}. TraceCorrelationId: {4}",
                                   this.ClientContext.ServerVersion,
                                   this.ClientContext.ServerSchemaVersion,
                                   this.ClientContext.ServerLibraryVersion,
                                   this.ClientContext.RequestSchemaVersion,
                                   this.ClientContext.TraceCorrelationId);

                //Try retrieving the SharePoint Server build version
                this.BuildVersion = TryGetServerVersion(site.Url);
            }
            catch (FileNotFoundException ex)
            {
                LogUtil.LogException(string.Format("File '{0}' not found, check log file {1} for detailed information.", ex.FileName, ex.FusionLog), ex);

                throw;
            }
        }