/// <summary> /// Initializes the control with the specified connection (name). /// </summary> /// <param name="connectionName">Name of the connection.</param> /// <param name="connection">The connection.</param> /// <remarks>Documented by Dev05, 2009-03-03</remarks> public UserStruct?Initialize(ConnectionStringStruct connection, UserStruct lastUser) { ConnectionString = LearningModulesIndex.ConnectionsHandler.GetConnectionFromConnectionStringStruct(connection); if (ConnectionString == null) { LearningModulesIndexEntry entry = RecentLearningModules.GetRecentModules().Find(m => m.ConnectionString.ConnectionString == connection.ConnectionString); if (entry != null) { ConnectionString = entry.Connection; } } ConnectionName = connection.Typ != DatabaseType.MsSqlCe ? (ConnectionString != null ? ConnectionString.Name : Resources.NO_CONNECTION_NAME) : Path.GetFileName(connection.ConnectionString); Connection = connection; if (connection.Typ == DatabaseType.MsSqlCe) { comboBoxUserSelection.DropDownStyle = ComboBoxStyle.DropDownList; } else { comboBoxUserSelection.DropDownStyle = ComboBoxStyle.DropDown; } #region error handling switch (lastUser.LastLoginError) { case LoginError.AlreadyLoggedIn: EmulatedTaskDialog taskDialog = new EmulatedTaskDialog(); taskDialog.Title = Resources.TASKDIALOG_KICK_TITLE; taskDialog.MainInstruction = string.Format(Resources.TASKDIALOG_KICK_MAIN, lastUser.UserName); taskDialog.Content = string.Format(Resources.TASKDIALOG_KICK_CONTENT, ConnectionName); taskDialog.CommandButtons = Resources.TASKDIALOG_KICK_BUTTONS; taskDialog.VerificationText = string.Empty; taskDialog.VerificationCheckBoxChecked = false; taskDialog.Buttons = TaskDialogButtons.None; taskDialog.MainIcon = TaskDialogIcons.Warning; taskDialog.FooterIcon = TaskDialogIcons.Warning; taskDialog.MainImages = new Image[] { Resources.system_log_out, Resources.processStopBig }; taskDialog.HoverImages = new Image[] { Resources.system_log_out, Resources.processStopBig }; taskDialog.CenterImages = true; taskDialog.BuildForm(); DialogResult dialogResult = taskDialog.ShowDialog(); if (dialogResult != DialogResult.Cancel && taskDialog.CommandButtonClickedIndex == 0) { lastUser.CloseOpenSessions = true; return(lastUser); } else { AllowAutoLogin = false; } break; case LoginError.InvalidUsername: TaskDialog.MessageBox(Resources.TASKDIALOG_WRONG_USER_TITLE, Resources.TASKDIALOG_WRONG_USER_MAIN, Resources.TASKDIALOG_WRONG_USER_CONTENT, TaskDialogButtons.OK, TaskDialogIcons.Error); break; case LoginError.InvalidPassword: TaskDialog.MessageBox(Resources.TASKDIALOG_WRONG_PASSWORD_TITLE, Resources.TASKDIALOG_WRONG_PASSWORD_MAIN, Resources.TASKDIALOG_WRONG_PASSWORD_CONTENT, TaskDialogButtons.OK, TaskDialogIcons.Error); break; case LoginError.WrongAuthentication: case LoginError.ForbiddenAuthentication: TaskDialog.MessageBox(Resources.TASKDIALOG_WRONG_AUTHENTIFICATION_TITLE, Resources.TASKDIALOG_WRONG_AUTHENTIFICATION_MAIN, Resources.TASKDIALOG_WRONG_AUTHENTIFICATION_CONTENT, TaskDialogButtons.OK, TaskDialogIcons.Error); break; case LoginError.NoError: default: break; } #endregion labelHeader.Text = string.Format(Resources.LOGIN_HEADER_TEXT, ConnectionName); buttonCancel.Enabled = connection.Typ != DatabaseType.MsSqlCe; try { SetListItems(); UpdateSelection(false); #region persitancy Stream outputStream = null; try { BinaryFormatter binary = new BinaryFormatter(); IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForAssembly(); outputStream = new IsolatedStorageFileStream(Settings.Default.AuthDataFile, FileMode.Open, storageFile); outputStream = new CryptoStream(outputStream, Rijndael.Create().CreateDecryptor(Encoding.Unicode.GetBytes("mlifter"), Encoding.Unicode.GetBytes("omicron")), CryptoStreamMode.Read); persitancyData = binary.Deserialize(outputStream) as Dictionary <string, SavedUserData>; outputStream.Close(); } catch { persitancyData = new Dictionary <string, SavedUserData>(); } finally { if (outputStream != null) { outputStream.Close(); } } if (ConnectionString != null && persitancyData.ContainsKey(ConnectionString.ConnectionString)) { SavedUserData data = persitancyData[ConnectionString.ConnectionString]; if (data.SaveUsername) { checkBoxSaveUsername.Checked = true; ShowOptions = true; comboBoxUserSelection.Text = data.Username; if (data.SavePassword) { checkBoxSavePassword.Checked = true; textBoxPassword.Text = data.Password; textBoxPassword.Tag = PasswordType.Hashed; if (data.AutoLogin) { checkBoxAutoLogin.Checked = true; if (AllowAutoLogin && !MLifter.BusinessLayer.User.PreventAutoLogin) { Login(); return(GetActualUser()); } } else { checkBoxAutoLogin.Checked = false; } } else { checkBoxSavePassword.Checked = false; } } else { checkBoxSaveUsername.Checked = false; ShowOptions = false; } } #endregion comboBoxUserSelection.Focus(); if (lastUser.LastLoginError != LoginError.NoError) { comboBoxUserSelection.Text = lastUser.UserName; textBoxPassword.Text = string.Empty; textBoxPassword.Tag = PasswordType.ClearText; } LoginError = lastUser.LastLoginError; } catch (ConnectionNotSetException ex) { Debug.WriteLine(ex.ToString()); } return(null); }