示例#1
0
        private void OnCheckTokenValidityCompleted(object sender, CheckTokenValidityCompletedEventArgs e)
        {
            _signInService.CheckTokenValidityCompleted -= OnCheckTokenValidityCompleted;
            if (e.Error != null)
            {
                return;
            }
            // current token is still valid.
            if (e.IsValid)
            {
                this.OnLoginAsyncCompleted(e.Token);
            }
            else
            {
                // need to get a new token.

                // we need to check if the settings are okay too because this takes place before the check performed in the OnSignIn command handler.
                var formatValidity = _synoSettings.IsCredentialFormatValid();

                if (formatValidity != CredentialFormatValidationResult.Valid)
                {
                    _signInService.ShowCredentialErrorMessage(formatValidity);
                    return;
                }

                _audioStationSession.Host = Host;
                _audioStationSession.Port = Port;

                try
                {
                    _audioStationSession.LoginAsync(this.UserName, this.Password, this.OnLoginAsyncCompleted,
                                                    this.OnLoginAsyncException, this._synoSettings.UseSsl);
                }
                catch (ArgumentNullException exception)
                {
                    // FIXME : Use noification service instead
                    MessageBox.Show(
                        "The connection settings don't look valid, please make sure they are entered correctly.",
                        "Credentials not valid",
                        MessageBoxButton.OK);
                }
                catch (UriFormatException exception)
                {
                    // FIXME : Use noification service instead
                    MessageBox.Show(
                        "The format of the provided hostname is not valid. Check that it is not prefixed it with http:// or https://",
                        "The host name is not valid",
                        MessageBoxButton.OK);
                }
            }
        }
示例#2
0
        public void CheckCachedTokenValidityAsync()
        {
            // no cached token
            if (CurrentTokenExistsForCurrentHost() == false)
            {
                if (CheckTokenValidityCompleted != null)
                {
                    CheckTokenValidityCompleted(this, new CheckTokenValidityCompletedEventArgs {
                        IsValid = false, Token = null
                    });
                }
                return;
            }
            string    token  = _openSynoSettings.Token;
            WebClient client = new WebClient();

            client.Headers["Cookie"] = token;
            // client.Headers["Accept-Encoding"] = "gzip, deflate";
            client.DownloadStringCompleted += (s, e) =>
            {
                JObject jobject = null;
                try
                {
                    jobject = JObject.Parse(e.Result);
                    var isValid = jobject["success"].Value <bool>();
                    _logService.Trace("CheckCachedTokenValidityAsync : token is valid : " + isValid.ToString());
                    if (CheckTokenValidityCompleted != null)
                    {
                        CheckTokenValidityCompleted(this, new CheckTokenValidityCompletedEventArgs {
                            IsValid = isValid, Token = token, Error = null
                        });
                    }
                }
                catch (WebException exception)
                {
                    _notificationService.Error("Please check that the specified hostname for the Disk Station is correct and that you are connected to the Internet.", "We can't connect to your Disk Station");
                    CheckTokenValidityCompleted(this, new CheckTokenValidityCompletedEventArgs {
                        IsValid = false, Token = null, Error = exception
                    });
                }
            };
            // we pass the client object along just so it doesn't get garbage collected before the eventhandler is called.
            var formatValidity = _openSynoSettings.IsCredentialFormatValid();

            if (formatValidity != CredentialFormatValidationResult.Valid)
            {
                ShowCredentialErrorMessage(formatValidity);
            }

            // var isBadFormat = CheckHostnameDoesNotContainPort(_openSynoSettings.Host);
            if (formatValidity == CredentialFormatValidationResult.Valid)
            {
                // let's prevent unexpected validation error.
                try
                {
                    string audioStationWebserviceRelativePath = this._versionDependentResourcesProvider.GetAudioStationWebserviceRelativePath(_openSynoSettings.DsmVersion);
                    string uriString = string.Format("http://{0}:{1}{2}?action=avoid_timeout", this._openSynoSettings.Host, this._openSynoSettings.Port, audioStationWebserviceRelativePath);
                    client.DownloadStringAsync(new Uri(uriString), client);
                }
                catch (UriFormatException e)
                {
                    _notificationService.Error("The credentials could not be parsed. Please check that the provided connection information do not contain any invalid characters.", "Invalid connection info");
                    return;
                }
            }
        }