Example #1
0
        public static bool Move(IHierarchyItem item, IFolder target, string newName,
                                bool overwrite, LockCollection locks)
        {
            string sourcePath                 = item.Href.AbsolutePath;
            bool   isSourceFolder             = item is IFolder;
            List <LockUriTokenPair> lockPairs = GetLockPairs(item, locks);

            try
            {
                item.MoveTo(target, newName, overwrite,
                            (lockPairs != null) ? lockPairs.ToArray() : null);
                ChangeLocks(sourcePath, isSourceFolder, target, newName, locks);
                return(true);
            }
            catch (WebDavException ex)
            {
                WebDavHttpException exept = ex as WebDavHttpException;
                if (exept == null || exept.Multistatus.Responses.Length == 0)
                {
                    Informator.ShowError(item.DisplayName + ": " + ex.Message);
                }
                else
                {
                    return(false);
                }
                return(false);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #2
0
 public static bool UpdateToVersion(IResource item, IVersion version, string lockToken)
 {
     try
     {
         if (lockToken != null)
         {
             item.UpdateToVersion(version, lockToken);
         }
         else
         {
             item.UpdateToVersion(version);
         }
         return(true);
     }
     catch (Exception e)
     {
         WebDavHttpException ex = e as WebDavHttpException;
         string status          = (ex != null && ex.Status != null)
                             ? "\nStatus: " + ex.Status.Description
                             : string.Empty;
         Informator.ShowError(item.DisplayName + ": " + e.Message + status);
         return(false);
     }
 }
Example #3
0
        /// <summary>
        /// Event handler to process WebDAV errors.
        /// If server returns 401 or 302 response here we show the login dialog.
        /// </summary>
        /// <param name="sender">Request to the WebDAV server.</param>
        /// <param name="e">WebDAV error details.</param>
        private static void DavClient_WebDavError(IWebRequestAsync sender, WebDavErrorEventArgs e)
        {
            WebDavHttpException httpException = e.Exception as WebDavHttpException;

            log.Info($"\n{httpException?.Status.Code} {httpException?.Status.Description} {e.Exception.Message} ");
            if (httpException != null)
            {
                switch (httpException.Status.Code)
                {
                // 302 redirect to login page.
                case 302:
                    if (loginRetriesCurrent < loginRetriesMax)
                    {
                        loginRetriesCurrent++;

                        // Show login dialog.

                        // Azure AD can not navigate directly to login page - failed corelation.
                        //string loginUrl = ((Redirect302Exception)e.Exception).Location;
                        //Uri url = new System.Uri(loginUrl, System.UriKind.Absolute);

                        Uri failedUri = (e.Exception as WebDavHttpException).Uri;

                        WebDAVDrive.UI.WebBrowserLogin webBrowserLogin = null;
                        Thread thread = new Thread(() => {
                            webBrowserLogin       = new WebDAVDrive.UI.WebBrowserLogin(failedUri, e.Request, DavClient, log);
                            webBrowserLogin.Title = Settings.ProductName;
                            webBrowserLogin.ShowDialog();
                        });
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();
                        thread.Join();

                        /*
                         * if (loginForm.Cookies != null)
                         * {
                         *  // Attach cookies to all future requests.
                         *  DavClient.CookieContainer.Add(loginForm.Cookies);
                         *  e.Result = WebDavErrorEventResult.Fail;
                         *
                         *  // Set successful response and continue processing.
                         *  e.Response = loginForm.Response;
                         *  e.Result = WebDavErrorEventResult.ContinueProcessing;
                         *
                         *  // Alternatively you can modify this request, attaching cookies or headers, and replay it.
                         *  //e.Request.CookieContainer.Add(loginForm.Cookies);
                         *  //e.Result = WebDavErrorEventResult.Repeat;
                         * }
                         */
                    }
                    break;

                // Challenge-responce auth: Basic, Digest, NTLM or Kerberos
                case 401:
                    if (loginRetriesCurrent < loginRetriesMax)
                    {
                        Uri failedUri = (e.Exception as WebDavHttpException).Uri;
                        Windows.Security.Credentials.PasswordCredential passwordCredential = CredentialManager.GetCredentials(Settings.ProductName, log);
                        if (passwordCredential != null)
                        {
                            passwordCredential.RetrievePassword();
                            DavClient.Credentials = new NetworkCredential(passwordCredential.UserName, passwordCredential.Password);
                        }
                        else
                        {
                            string       login        = null;
                            SecureString password     = null;
                            bool         dialogResult = false;
                            bool         keepLogedin  = false;

                            // Show login dialog
                            WebDAVDrive.UI.ChallengeLogin loginForm = null;
                            Thread thread = new Thread(() =>
                            {
                                loginForm = new WebDAVDrive.UI.ChallengeLogin();
                                ((ChallengeLoginViewModel)loginForm.DataContext).Url         = failedUri.OriginalString;
                                ((ChallengeLoginViewModel)loginForm.DataContext).WindowTitle = Settings.ProductName;
                                loginForm.ShowDialog();

                                login        = ((ChallengeLoginViewModel)loginForm.DataContext).Login;
                                password     = ((ChallengeLoginViewModel)loginForm.DataContext).Password;
                                keepLogedin  = ((ChallengeLoginViewModel)loginForm.DataContext).KeepLogedIn;
                                dialogResult = (bool)loginForm.DialogResult;
                            });
                            thread.SetApartmentState(ApartmentState.STA);
                            thread.Start();
                            thread.Join();

                            loginRetriesCurrent++;
                            if (dialogResult)
                            {
                                if (keepLogedin)
                                {
                                    CredentialManager.SaveCredentials(Settings.ProductName, login, password);
                                }
                                DavClient.Credentials = new NetworkCredential(login, password);
                            }
                        }
                    }
                    break;
                }
            }
        }