コード例 #1
0
ファイル: formMain.cs プロジェクト: Healix/Gw2Launcher
 private DialogResult OnPasswordRequired(string username)
 {
     try
     {
         BeforeShowDialog();
         activeWindows++;
         using (formPassword f = new formPassword("Password for " + username + ":"))
         {
             while (true)
             {
                 if (f.ShowDialog(this) != DialogResult.OK || f.Password.Length == 0)
                 {
                     if (MessageBox.Show(this, "A password is required to use this account", "Password required", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation) != System.Windows.Forms.DialogResult.Retry)
                         return DialogResult.Cancel;
                 }
                 else
                 {
                     Security.Credentials.SetPassword(username, f.Password);
                     return DialogResult.OK;
                 }
             }
         }
     }
     finally
     {
         activeWindows--;
     }
 }
コード例 #2
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            buttonOK.Enabled = false;
            listAccounts.Enabled = false;
            textAccountName.Enabled = false;

            try
            {
                if (string.IsNullOrWhiteSpace(textAccountName.Text))
                {
                    this.AccountName = null;
                    this.DialogResult = DialogResult.OK;
                    return;
                }

                Principal p = GetAccount(textAccountName.Text);
                if (p != null)
                {
                    this.AccountName = textAccountName.Text;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    DialogResult result = MessageBox.Show(this, "A user with the name \"" + textAccountName.Text + "\" could not be found.\n\nWould you like to create this user now?", "User not found, create?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (result)
                    {
                        case System.Windows.Forms.DialogResult.Cancel:
                            return;
                        case System.Windows.Forms.DialogResult.Yes:
                            formPassword f;
                            System.Security.SecureString password;

                            using (f = new formPassword("New password"))
                            {
                                if (f.ShowDialog(this) != DialogResult.OK)
                                    return;

                                password = f.Password.Copy();

                                if (password.Length == 0)
                                {
                                    password.Dispose();
                                    MessageBox.Show(this, "A password is required to use another user's account", "Password required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                            }

                            using (password)
                            {
                                using (f = new formPassword("Confirm password"))
                                {
                                    if (f.ShowDialog(this) != DialogResult.OK)
                                        return;

                                    if (!Security.Credentials.Compare(password, f.Password))
                                    {
                                        MessageBox.Show(this, "Passwords do not match", "Wrong password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        return;
                                    }
                                }

                                if (!CreateAccount(textAccountName.Text, password))
                                {
                                    MessageBox.Show(this, "An error occured while trying to create the user", "Unable to create the user", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                                else
                                {
                                    Security.Credentials.SetPassword(textAccountName.Text, password);
                                    try
                                    {
                                        Util.ProcessUtil.InitializeAccount(textAccountName.Text, password);
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(this, "The user has been successfully created, but you will need to first login to the account before it can be used.", "User created", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                }
                            }

                            this.AccountName = textAccountName.Text;
                            this.DialogResult = DialogResult.OK;
                            break;
                        case System.Windows.Forms.DialogResult.No:
                            this.AccountName = textAccountName.Text;
                            this.DialogResult = DialogResult.OK;
                            break;
                    }
                }
            }
            finally
            {
                buttonOK.Enabled = true;
                textAccountName.Enabled = true;
                listAccounts.Enabled = true;
            }
        }
コード例 #3
0
ファイル: formAccount.cs プロジェクト: Healix/Gw2Launcher
        private Settings.IDatFile GetDatFile()
        {
            if (selectedFile == null)
            {
                if (account != null)
                    return account.DatFile;
                else
                    return null;
            }
            else if (selectedFile.DatFile != null)
            {
                return selectedFile.DatFile;
            }
            else if (selectedFile.Path == null)
            {
                return null;
            }

            Security.Impersonation.IImpersonationToken impersonation = null;

            string username = Util.Users.GetUserName(textWindowsAccount.Text);
            bool isCurrent = Util.Users.IsCurrentUser(username);
            bool userExists = isCurrent;
            bool wasMoved = false;

            string path = null;

            if (!isCurrent)
            {
                try
                {
                    using (var user = Util.Users.GetPrincipal(username))
                    {
                        userExists = user != null;
                    }
                }
                catch { }

                if (userExists)
                {
                    var password = Security.Credentials.GetPassword(username);

                    while (true)
                    {
                        if (password == null)
                        {
                            using (formPassword f = new formPassword("Password for " + username))
                            {
                                if (f.ShowDialog(this) == DialogResult.OK)
                                {
                                    password = f.Password;
                                    Security.Credentials.SetPassword(username, password);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }

                        try
                        {
                            impersonation = Security.Impersonation.Impersonate(username, Security.Credentials.GetPassword(username));
                            break;
                        }
                        catch (Win32Exception ex)
                        {
                            if (ex.NativeErrorCode == 1326)
                            {
                                password = null;
                                continue;
                            }

                            break;
                        }
                        catch (Exception ex)
                        {
                            break;
                        }
                    }
                }
            }

            if (isCurrent || impersonation != null)
            {
                try
                {
                    string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create);
                    if (!string.IsNullOrWhiteSpace(folder))
                    {
                        folder = Path.Combine(folder, "Guild Wars 2");
                        if (!Directory.Exists(folder))
                            Directory.CreateDirectory(folder);

                        path = Util.FileUtil.GetTemporaryFileName(folder);

                        try
                        {
                            File.Move(selectedFile.Path, path);
                            wasMoved = true;
                        }
                        catch
                        {
                            try
                            {
                                File.Copy(selectedFile.Path, path, true);
                            }
                            catch (Exception e)
                            {
                                throw e;
                            }
                        }
                    }
                    else
                    {
                        //the user exists, but the account hasn't been set up yet
                    }
                }
                finally
                {
                    if (impersonation != null)
                        impersonation.Dispose();
                }
            }

            if (datFile == null)
                datFile = Settings.CreateDatFile();

            if (path == null)
                datFile.Path = Path.GetFullPath(selectedFile.Path);
            else
            {
                try
                {
                    //now that datFile setting has been created, rename the file from its temp name
                    string _path = Path.Combine(Path.GetDirectoryName(path), "Local." + datFile.UID + ".dat");
                    File.Move(path, _path);
                    path = _path;
                }
                catch { }
                datFile.Path = Path.GetFullPath(path);
            }

            if (!wasMoved)
            {
                try
                {
                    File.Delete(selectedFile.Path);
                }
                catch { }
            }

            return datFile;
        }
コード例 #4
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            buttonOK.Enabled        = false;
            listAccounts.Enabled    = false;
            textAccountName.Enabled = false;

            try
            {
                if (string.IsNullOrWhiteSpace(textAccountName.Text))
                {
                    this.AccountName  = null;
                    this.DialogResult = DialogResult.OK;
                    return;
                }

                Principal p = GetAccount(textAccountName.Text);
                if (p != null)
                {
                    this.AccountName  = textAccountName.Text;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    DialogResult result = MessageBox.Show(this, "A user with the name \"" + textAccountName.Text + "\" could not be found.\n\nWould you like to create this user now?", "User not found, create?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (result)
                    {
                    case System.Windows.Forms.DialogResult.Cancel:
                        return;

                    case System.Windows.Forms.DialogResult.Yes:
                        formPassword f;
                        System.Security.SecureString password;

                        using (f = new formPassword("New password"))
                        {
                            if (f.ShowDialog(this) != DialogResult.OK)
                            {
                                return;
                            }

                            password = f.Password.Copy();

                            if (password.Length == 0)
                            {
                                password.Dispose();
                                MessageBox.Show(this, "A password is required to use another user's account", "Password required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                return;
                            }
                        }

                        using (password)
                        {
                            using (f = new formPassword("Confirm password"))
                            {
                                if (f.ShowDialog(this) != DialogResult.OK)
                                {
                                    return;
                                }

                                if (!Security.Credentials.Compare(password, f.Password))
                                {
                                    MessageBox.Show(this, "Passwords do not match", "Wrong password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                            }

                            if (!CreateAccount(textAccountName.Text, password))
                            {
                                MessageBox.Show(this, "An error occured while trying to create the user", "Unable to create the user", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            else
                            {
                                Security.Credentials.SetPassword(textAccountName.Text, password);
                                try
                                {
                                    Util.ProcessUtil.InitializeAccount(textAccountName.Text, password);
                                }
                                catch (Exception ex)
                                {
                                    Util.Logging.Log(ex);
                                    MessageBox.Show(this, "The user has been successfully created, but you will need to first login to the account before it can be used.", "User created", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                            }
                        }

                        this.AccountName  = textAccountName.Text;
                        this.DialogResult = DialogResult.OK;
                        break;

                    case System.Windows.Forms.DialogResult.No:
                        this.AccountName  = textAccountName.Text;
                        this.DialogResult = DialogResult.OK;
                        break;
                    }
                }
            }
            finally
            {
                buttonOK.Enabled        = true;
                textAccountName.Enabled = true;
                listAccounts.Enabled    = true;
            }
        }