2009-08-01 5 views
0

winform 응용 프로그램과 일부 웹 서비스에 대한 로그인 목록이 있습니다. 확인되면 나는 암호화 된 비밀번호와 함께 파일에 사전을 직렬화,하지만 그런 일 또는하지를 할 수있는 가장 좋은 방법이 있는지 궁금해 '나를 기억'.. 다음은 내 코드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); 
} 
+0

나를 기억하는 기능이 시간이 지남에 따라 양호합니까 (응용 프로그램을 닫고 다시 열 때) 또는 응용 프로그램의 현재 기능에 대한 것입니까? – CertifiedCrazy

+0

시간이 지남에 좋습니다. LoginsInit()가 intance 생성시에 호출되어 저장된 파일에서 dictoinary를 인스턴스화합니다 – mustafabar

답변

1

설정을 응용 프로그램 인스턴스 전체에 유지하려는 경우 .NET에 내장 된 Application Settings Architecture을 확인해야합니다. 이는 지속성과 재로드 기능을 제공합니다 (일부 구성 필요). 추가 보안 및 기능을 위해 Isolated Storage도 있습니다. 사용하는 것에 관계없이 비밀번호를 계속 암호화하십시오.

3

당신은 수도 있어요 DPAPI을 사용하여 키를 관리하는 것이 좋습니다.

0

암호와 같은 중요한 정보를 관리 할 때는 SecureString 클래스 을 사용하여 자격 증명을 저장하는 것이 좋습니다.

관련 문제