Example #1
0
        private async Task Login(RemoteDatabase database)
        {
            try
            {
                if (!this.Client.IsLoggedIn)
                {
                    var autoLogin = this.Configuration.AutoLogin;
                    var authenticationTokenUrl = this.Configuration.AllorsAuthenticationTokenUrl;

                    if (!string.IsNullOrWhiteSpace(autoLogin))
                    {
                        var user = this.Configuration.AutoLogin;
                        var uri  = new Uri(authenticationTokenUrl, UriKind.Relative);
                        this.Client.IsLoggedIn = await database.Login(uri, user, null);

                        if (this.Client.IsLoggedIn)
                        {
                            this.Client.UserName = user;
                        }
                    }
                    else
                    {
                        if (!this.Client.IsLoggedIn)
                        {
                            using var loginForm = new LoginForm
                                  {
                                      Database = database,
                                      Uri      = new Uri(authenticationTokenUrl, UriKind.Relative)
                                  };

                            var result = loginForm.ShowDialog();

                            if (result == DialogResult.OK)
                            {
                                this.Client.UserName   = loginForm.UserName;
                                this.Client.IsLoggedIn = true;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.Handle();
            }
        }
        private async Task Login(RemoteDatabase database)
        {
            try
            {
                if (!this.Client.IsLoggedIn)
                {
                    var autologin = this.Configuration.AutoLogin;

                    if (!string.IsNullOrWhiteSpace(autologin))
                    {
                        var user = this.Configuration.AutoLogin;
                        var uri  = new Uri("/TestAuthentication/Token", UriKind.Relative);
                        this.Client.IsLoggedIn = await database.Login(uri, user, null);

                        if (this.Client.IsLoggedIn)
                        {
                            this.Client.UserName = user;
                        }
                    }
                    else
                    {
                        // Check if there is some sort of automated login
                        var uri = new Uri("/Ping/Token", UriKind.Relative);

                        HttpResponseMessage response = null;
                        try
                        {
                            response = await database.PostAsJsonAsync(uri, null);

                            if (response.IsSuccessStatusCode)
                            {
                                this.Client.IsLoggedIn = true;
                            }
                        }
                        catch (Exception)
                        {
                            // Catch the Unauthorized exception only
                            if (response?.StatusCode != HttpStatusCode.Unauthorized)
                            {
                                throw;
                            }
                        }

                        if (!this.Client.IsLoggedIn)
                        {
                            using (var loginForm = new LoginForm())
                            {
                                loginForm.Database = database;
                                loginForm.Uri      = new Uri("/TestAuthentication/Token", UriKind.Relative);
                                var result = loginForm.ShowDialog();
                                if (result == DialogResult.OK)
                                {
                                    this.Client.UserName   = loginForm.UserName;
                                    this.Client.IsLoggedIn = true;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.Handle();
            }
        }