Passwords in C#
Can anyone show me how to get, store and read usenames and passwords inputted by a user? Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
StefanPosted Jan 23, 2009, 11:34 AM
Heres my code:
This is from one form that asks for the username and password and then writes it to a file:
private void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text;
string password = textBox2.Text;
WriteUserAndPassword(username, password);
}
private void WriteUserAndPassword(string user, string pass)
{
using (StreamWriter writer = File.AppendText("C:\\pass.txt"))
{
writer.WriteLine("username:");
writer.WriteLine(user);
writer.WriteLine("password:");
writer.WriteLine(pass);
writer.Dispose();
writer.Close();
}
}
This code is for the form that asks the user to login:
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader("C:\\pass.txt"))
{
string username = null;
string password = null;
string content = null;
while (reader.ReadLine() != null)
{
content = reader.ReadLine();
if (textBox1.Text == content)
{
username = content;
}
reader.ReadLine();
content = reader.ReadLine();
if (textBox2.Text == content)
{
password = content;
}
if ((textBox1.Text == username) && (textBox2.Text == password))
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
}
}
if ((textBox1.Text != username)||(textBox2.Text != password))
{
MessageBox.Show("Username or password is incorrect");
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
}
}
I realize the code is very sloppy. I had a hard time finding good tutorials for this.
Any help will be appreciated.
PP9394Posted Jan 22, 2009, 8:55 PM
it is a base use for every application,u can find it easily by google.
but try it youself is the best way
Ryan AlfordPosted Jan 22, 2009, 6:43 PM
StefanPosted Jan 22, 2009, 5:22 PM