Prachur
Prachur

Reputation: 1110

DotNet :- How to save username & password?

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

Upvotes: 0

Views: 1682

Answers (4)

dannyyy
dannyyy

Reputation: 1784

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

Upvotes: 1

dhirschl
dhirschl

Reputation: 2098

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

Upvotes: 1

tchrikch
tchrikch

Reputation: 2468

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

Upvotes: 1

Anuraj
Anuraj

Reputation: 19598

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 ;)

Upvotes: 2

Related Questions