title: How to save logins in a winform application?
body: <p>I have a winform application and a list of logins to some web-service. Upon checking 'remember me' I serialize a dictionary into a file along with the encrypted password, but I wonder if this is the best practice to do such a thing or not..
  Here's my code </p>
  
  <pre><code>public void LoginsInit()
  {
    FileStream file = new FileStream(loginsFilePath, FileMode.OpenOrCreate);
    try
    {
      BinaryFormatter formatter = new BinaryFormatter();
      loginsDictionary = (Dictionary&lt;string, string&gt;)formatter.Deserialize(file);
      string[] allusers = loginsDictionary.Keys.ToArray();
      int usersCount = allusers.Length;
      userNameTextBox.Text = allusers[usersCount - 1];
    }
    catch (SerializationException ex)
    {
      loginsDictionary = new Dictionary&lt;string, string&gt;();
      Console.WriteLine("Failed to open file: " + ex.Message);
    }
    finally
    {
      file.Close();
    }
  }
  
  private void login_Click(object sender, EventArgs e)
  {
    //LoginToService();
    string username;
    string password;
    username = serviceClientReference.UserLogin = userNameTextBox.Text;
    password = serviceClientReference.Password = EncryptDecrypt.Encrypt(this.passwordTextBox.Text, EncryptDecrypt.c_strEncryptkey1, EncryptDecrypt.c_strEncryptkey2);
  
    if (rememberMe.Checked)
    {
      if (loginsDictionary.ContainsKey(username))
        loginsDictionary[username] = password;
      else
        loginsDictionary.Add(username, password);
    }
    FileStream file = new FileStream(loginsFilePath, FileMode.Create);
    try
    {
      BinaryFormatter formatter = new BinaryFormatter();
      formatter.Serialize(file, loginsDictionary);
      file.Flush();
    }
    catch (SerializationException ex)
    {
      Console.WriteLine("Failed to open file: " + ex.Message);
    }
    finally
    {
      file.Close();
    }
  
    string errorStr;
    int errorNo;
    try
    {
      bool res = serviceClientReference.EstablishConnection(out errorStr, out errorNo);
      if (!res)
      {
        MessageBox.Show(errorStr);
      }
    }
    catch (Exception exception)
    {
      Logger.Log(TraceLevel.Error, "", exception);
      MessageBox.Show("Fatal Error Unable to login to MU");
    }
  }
  
  private void usernameTextBox_TextChanged(object sender, EventArgs e)
  {
    if (loginsDictionary.ContainsKey(userNameTextBox.Text))
      passwordTextBox.Text = EncryptDecrypt.Decrypt(loginsDictionary[userNameTextBox.Text], EncryptDecrypt.c_strEncryptkey1, EncryptDecrypt.c_strEncryptkey2);
  }
  </code></pre>