コード例 #1
0
        public bool LogIn(Window owner, out string loginToken)
        {
            loginToken = null;

            if (ReadFactorioCredentials(out username, out token))
            {
                loginToken = token;
                return(true);
            }

            if (IsLoggedInWithToken()) // Credentials and token available.
            {
                // Token only expires on user request, best to not check and save bandwidth.
            }
            else if (IsLoggedIn()) // Only credentials available.
            {
                AuthenticationInfo info = ApiAuthentication.LogIn(Username, Token);
                username = info.Username;
                token    = info.Token;
                if (App.Instance.Settings.SaveCredentials)
                {
                    Save();
                }
            }


            while (!IsLoggedInWithToken())
            {
                var loginWindow = new LoginWindow
                {
                    Owner = owner,
                    SaveCredentialsBox = { IsChecked = App.Instance.Settings.SaveCredentials },
                };
                bool?loginResult = loginWindow.ShowDialog();
                if (loginResult == null || loginResult == false)
                {
                    return(false);
                }
                username = loginWindow.UsernameBox.Text;
                token    = loginWindow.TokenBox.Text;
                bool saveCredentials = loginWindow.SaveCredentialsBox.IsChecked ?? false;
                App.Instance.Settings.SaveCredentials = saveCredentials;

                AuthenticationInfo info = ApiAuthentication.LogIn(Username, Token);
                username = info.Username;
                token    = info.Token;
                if (saveCredentials)
                {
                    Save();
                }
            }

            loginToken = token;
            return(true);
        }
コード例 #2
0
        public bool LogIn(Window owner, out string loginToken)
        {
            loginToken = null;

            bool failed = false;

            if (IsLoggedInWithToken()) // Credentials and token available.
            {
                // ToDo: check if token is still valid (does it actually expire?).
            }
            else if (IsLoggedIn()) // Only credentials available.
            {
                failed = !ApiAuthentication.LogIn(Username, Password, out token);
            }

            if (failed)
            {
                token     = null;
                container = null;
            }

            while (!IsLoggedInWithToken())
            {
                var loginWindow = new LoginWindow
                {
                    Owner = owner,
                    SaveCredentialsBox = { IsChecked = App.Instance.Settings.SaveCredentials },
                    FailedText         = { Visibility = failed ? Visibility.Visible : Visibility.Collapsed },
                };
                bool?loginResult = loginWindow.ShowDialog();
                if (loginResult == null || loginResult == false)
                {
                    return(false);
                }
                username = loginWindow.UsernameBox.Text;
                password = loginWindow.PasswordBox.SecurePassword;

                bool saveCredentials = loginWindow.SaveCredentialsBox.IsChecked ?? false;
                App.Instance.Settings.SaveCredentials = saveCredentials;

                failed = !ApiAuthentication.LogIn(Username, Password, out token);
                if (failed)
                {
                    token     = null;
                    container = null;
                }
                else if (saveCredentials)
                {
                    Save();
                }
            }

            loginToken = token;
            return(true);
        }
コード例 #3
0
        public bool LogIn(Window owner, out string loginToken)
        {
            loginToken = null;

            bool failed = false;

            if (IsLoggedInWithToken()) // Credentials and token available.
            {
                // Token only expires on user request, best to not check and save bandwidth.
            }
            else if (IsLoggedIn()) // Only credentials available.
            {
                AuthenticationInfo info;
                failed = !ApiAuthentication.LogIn(Username, Password, out info);
                if (!failed)
                {
                    username = info.Username;
                    token    = info.Token;
                    if (App.Instance.Settings.SaveCredentials)
                    {
                        Save();
                    }
                }
            }

            if (failed)
            {
                token = null;
            }

            while (!IsLoggedInWithToken())
            {
                var loginWindow = new LoginWindow
                {
                    Owner = owner,
                    SaveCredentialsBox = { IsChecked = App.Instance.Settings.SaveCredentials },
                    FailedText         = { Visibility = failed ? Visibility.Visible : Visibility.Collapsed },
                };
                bool?loginResult = loginWindow.ShowDialog();
                if (loginResult == null || loginResult == false)
                {
                    return(false);
                }
                username = loginWindow.UsernameBox.Text;
                password = loginWindow.PasswordBox.SecurePassword;

                bool saveCredentials = loginWindow.SaveCredentialsBox.IsChecked ?? false;
                App.Instance.Settings.SaveCredentials = saveCredentials;

                AuthenticationInfo info;
                failed = !ApiAuthentication.LogIn(Username, Password, out info);
                if (failed)
                {
                    token = null;
                }
                else
                {
                    username = info.Username;
                    token    = info.Token;
                    if (saveCredentials)
                    {
                        Save();
                    }
                }
            }

            loginToken = token;
            return(true);
        }