public AddAccountViewModel(ConnectionService connectionService, AccountService accountService, INavigationService navigationService) {
            _connectionService = connectionService;
            _accountService = accountService;
            _navigationService = navigationService;

            AnonymousAccess = false;
            IsHostValid = false;

            CheckHostCommand = new RelayCommand(async () => {
                // TODO: check if really changed
                IsHostValid = false;
                var host = Host;
                if (!host.EndsWith("/")) host = host + "/";

                _connectionInfo = await _connectionService.GetConnectionInfo(host);
                if (_connectionInfo != null) {
                    var auth = await _connectionService.GetAuthenticationInfo(_connectionInfo);
                    if (auth != AuthenticationMethod.Unknown)
                        IsHostValid = true;
                    AnonymousAccess = auth == AuthenticationMethod.None;
                }
            });

            AddAccountCommand = new RelayCommand(async () => {
                var newAccount = new Account {
                    IsAnonymous = AnonymousAccess,
                    Protocol = _connectionInfo.WebDAVUrl.Scheme,
                    ServerDomain = _connectionInfo.WebDAVUrl.Host,
                    WebDAVPath = _connectionInfo.WebDAVUrl.LocalPath
                };

                if (!AnonymousAccess) {
                    newAccount.UsernamePlain = UserName;
                    newAccount.PasswordPlain = Password;
                    await _accountService.UpdateCredentials(newAccount);
                }
                _accountService.AddAccount(newAccount);
                await _accountService.SaveAccounts();

                if (_navigationService.CanGoBack)
                    _navigationService.GoBack();
                else
                    _navigationService.Navigate(typeof(MainPage));
            }, () => IsHostValid &&
                        (AnonymousAccess ||
                        (!string.IsNullOrWhiteSpace(UserName) &&
                             !string.IsNullOrEmpty(Password))));
        }
Example #2
0
 public async Task<AuthenticationMethod> GetAuthenticationInfo(ConnectionInfo connection) {
     var webdav = new WebDAVClient(connection.WebDAVUrl);
     var method = AuthenticationMethod.Unknown;
     try {
         await webdav.GetRootEntries(false);
         method = AuthenticationMethod.None;
     } catch (WebException webex) {
         if (webex != null && webex.Response is HttpWebResponse) {
             var webResponse = (HttpWebResponse)webex.Response;
             if (webResponse.StatusCode == HttpStatusCode.Unauthorized) {
                 var authHeader = webResponse.Headers["www-authenticate"].ToLower().Trim();
                 if (authHeader.StartsWith("basic"))
                     method = AuthenticationMethod.Basic;
             }
         }
     }
     return method;
 }