我有一个winform应用程序和一些web服务登录列表。在检查“记住我”我序列一本字典与加密密码沿文件,但我不知道这是做这样的事情还是不是最好的做法.. 这里是我的代码如何将登录保存在winform应用程序中?
public void LoginsInit()
{
FileStream file = new FileStream(loginsFilePath, FileMode.OpenOrCreate);
try
{
BinaryFormatter formatter = new BinaryFormatter();
loginsDictionary = (Dictionary<string, string>)formatter.Deserialize(file);
string[] allusers = loginsDictionary.Keys.ToArray();
int usersCount = allusers.Length;
userNameTextBox.Text = allusers[usersCount - 1];
}
catch (SerializationException ex)
{
loginsDictionary = new Dictionary<string, string>();
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);
}
记住我的功能是否随时间变化良好(应用程序关闭并重新打开)还是仅适用于当前的应用程序? – CertifiedCrazy 2009-08-01 10:27:57