开发者

DotNet :- How to save username & password?

开发者 https://www.devze.com 2023-02-17 07:28 出处:网络
I have a windows form application. there is a checkbox remember me. now i want if i check this box , then the username is saved in some file on local computer , so that it can be used after some time.

I have a windows form application. there is a checkbox remember me. now i want if i check this box , then the username is saved in some file on local computer , so that it can be used after some time. also how to read that file so that the username & password are automatically set开发者_如何学运维 when the form is started again


I don't know its good practice or not. But you can use Project -> Settings for this. So you can access the values as Properties. Make sure you are encrypting it ;)


Or another simple method is writing both in a text file. But don't forgot to encrypt the password.

You can decide to use the first line for username and the second line for password.


public void SaveUserPass(string username, string password)
{
  using (var credentialFile = new StreamWriter("credentials.txt", false))
  {
    credentialFile.WriteLine(username);
    credentialFile.WrtieLine(password); // Please save encrypted!
  }
}


public void ReadUserPass(out string username, out string password)
{
  using (var credentialFile = new StreamReader("credentials.txt", false))
  {
    var lines = credentialFile.ReadLine();
    username = lines[0];
    password = lines[1]; // don't forget to decrypt if you have encrypted before!
  }
}

Instead of using out parameters you can split the method for username and password or use properties of the same class or make your own class for the arguemtns. It's up to you. The code above is just a simple example!

Kind regards


First of all you can use ISO - isolated storage to store password, here's quick tutorial on how to work with it ISO

Another thing is that you should store encrypted password or a hash version of it but never keep password in pure form


I agree with the comment that a password should never be saved.

If you did want to store user settings related to an application, you can use Isolated Storage.

More Information: When to use IsolatedStorage

0

精彩评论

暂无评论...
验证码 换一张
取 消