lesPaul124
January 23rd, 2009, 11:11 AM
Can anyone show me how to create a form that asks a new user to create a user name and password, and then store that password. Then, how could I make a log in form? Any help would be appreciated.
|
Click to See Complete Forum and Search --> : Beginner needs help with passwords lesPaul124 January 23rd, 2009, 11:11 AM Can anyone show me how to create a form that asks a new user to create a user name and password, and then store that password. Then, how could I make a log in form? Any help would be appreciated. dannystommen January 23rd, 2009, 11:39 AM I'd suggest to do a tutorial first. BigEd781 January 23rd, 2009, 12:03 PM I agree. Don't mess around with things like that which require some knowledge before you know how to make a form. Or is this for school... funnyusername January 23rd, 2009, 04:56 PM Use a masked text box for the password and a normal one for the username. Save the data in a database file. On the log in form have the person enter the username and password, recall the data from your database, use a simple 'if' statement to validate it. At least thats how I'd do it. It's quite straightforward once you can interact with the database....which is not so straightforward IMHO. Unfortunately storing and retrieving data is a HUGE area, as I found out and was driven to pay over $500 in December to learn the basics of how to use databases after spending months banging my head against the proverbial wall! I'm neither experienced or qualified to try and teach it, and I'm afraid I can't recommend a good book, because I couldn't find any over months of trying. I also can't share the nine 9 tutorial videos I commissioned, because I agreed the seller could hold the rights to them. I imagine some form of encryption should also be used for this, but I haven't had to do it yet (Actually, writing code to encrypt strings of data facinates me, and I intend to try sometime, I don't think it would be difficult, you'd just have to keep the text-numeric conversions well hidden.....I think). The persons name who created the excellent videos for me was 'Vandad' and he's in the UK. You can find him here (Screen Name 'Vandad NP') http://www.rentacoder.com/RentACoder/DotNet/SoftwareCoders/ShowBioInfo.aspx?lngAuthorId=468702 I'm sure it would be much cheaper to buy the videos now that they are already created. Otherwise, if this is something your serious about, I can't recommend him enough. Sorry if this doesn't help too much..... BigEd781 January 23rd, 2009, 05:40 PM I imagine some form of encryption should also be used for this, but I haven't had to do it yet This is why we suggested some learning first. Security is hard for experienced developers, let alone beginners. Also, for the textbox, you can use a regular TextBox and set the UseSystemPasswordChar to true and the password will not be displayed in plain text. A masked textbox is really for validation. funnyusername January 24th, 2009, 10:54 AM Thanks Ed. :) lesPaul124 January 24th, 2009, 06:31 PM Okay. So I've figured out how to get the input, then hash the password. I also know how to then validate the usename and password at log in. I only know how to write it to a text file, though. I have three forms: one for log in, one for registration, and one for the main app. Here's my code for the form that writes the user data: private void button1_Click(object sender, EventArgs e) { string username = textBox1.Text; string password = hash_password(textBox2.Text); using (StreamWriter wr = System.IO.File.AppendText("C:\\pass.txt")) { wr.WriteLine(username + "," + password); } this.Close(); } static string hash_password(string to_be_hashed) { byte[] arrbyte = new byte[to_be_hashed.Length]; SHA1 hash = new SHA1CryptoServiceProvider(); arrbyte = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(to_be_hashed)); return Convert.ToBase64String(arrbyte); } and here's my log in form: private void button1_Click(object sender, EventArgs e) { using (StreamReader reader = new StreamReader("C:\\pass.txt")) { string[] allLines = File.ReadAllLines(@"C:\pass.txt"); string[] usernames = new string[allLines.Length]; string[] passwords = new string[allLines.Length]; for (int i = 0; i < allLines.Length; i++) { string[] lineSplit = allLines[i].Split(','); usernames[i] = lineSplit[0]; passwords[i] = lineSplit[1]; } string username = textBox1.Text; string password = hashed_password(textBox2.Text); for (int i = 0; i < usernames.Length; i++) if (usernames[i] == username) if (passwords[i] == password) { this.Hide(); Form3 frm = new Form3(); frm.Show(); return; } else { MessageBox.Show("Incorrect username/password."); return; } MessageBox.Show("Incorrect username/password."); } } static string hashed_password(string to_be_hashed) { byte[] arrbyte = new byte[to_be_hashed.Length]; SHA1 hash = new SHA1CryptoServiceProvider(); arrbyte = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(to_be_hashed)); return Convert.ToBase64String(arrbyte); } Can anyone show me how to modify this code so it writes toa database? Or, if this is not practical, can someone show me a simple example of how to connect to a database and store user data in it? Any help would be appreciated. funnyusername January 25th, 2009, 09:51 AM Please edit your post and put the command [code ] and [/code ] (Without the spaces before the ]) before and at the end of the code, it'll be much easier to read. :) Thanks. If its any use to you, I use MySQL, its free. http://www.mysql.com/ You need to download and install the database before you can use it on your local machine. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |