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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Stefan7 reputationPosted Jan 22, 2009, 12:22 PM EST
Ryan AlfordRank #1,411 · 1.2K reputationPosted Jan 22, 2009, 1:43 PM EST
PP93948 reputationPosted Jan 22, 2009, 3:55 PM EST
it is a base use for every application,u can find it easily by google.
but try it youself is the best way
Stefan7 reputationPosted Jan 23, 2009, 6:34 AM EST
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.