Got stuck on creating a login and password form. Epic If statement needed

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 40
Reputation: lolwtf is an unknown quantity at this point 
Solved Threads: 0
lolwtf lolwtf is offline Offline
Light Poster

Got stuck on creating a login and password form. Epic If statement needed

 
0
  #1
May 24th, 2009
I wanted to create a form where a user can create an account, have it loaded in SQL, and be able to retrieve the information and validate it. I've reached a mental block on the validation code and need some advice. Here is my code so far:

  1. Public Class main
  2.  
  3. Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblUsername.Click
  4.  
  5. End Sub
  6.  
  7. Private Sub btnJoin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJoin.Click
  8.  
  9.  
  10. lblName.Visible = True
  11. lblUsername.Visible = True
  12. lblPassword.Visible = True
  13. txtName.Visible = True
  14. txtUsername.Visible = True
  15. txtPassword.Visible = True
  16. btnNext.Visible = True
  17. End Sub
  18.  
  19. Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.  
  21. Try
  22. SqlConnection1.Open()
  23.  
  24. Catch ex As Exception
  25. MsgBox(ex.Message)
  26.  
  27.  
  28. End Try
  29.  
  30. End Sub
  31.  
  32. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  33. DBAuser.InsertCommand.CommandText = "INSERT INTO login (userName, login, password) VALUES ('" + txtName.Text.Replace("'", "") + "', '" + txtUsername.Text.Replace("'", "") + "', '" + txtPassword.Text.Replace("'", "") + "');"
  34. DBAuser.InsertCommand.ExecuteNonQuery()
  35. MessageBox.Show("Account Created Successfully")
  36. txtName.Clear()
  37. txtUsername.Clear()
  38. txtPassword.Clear()
  39.  
  40.  
  41. lblName.Visible = False
  42. lblUsername.Visible = False
  43. lblPassword.Visible = False
  44. txtName.Visible = False
  45. txtUsername.Visible = False
  46. txtPassword.Visible = False
  47. btnNext.Visible = False
  48.  
  49.  
  50.  
  51. End Sub
  52.  
  53. Private Sub btnUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUser.Click
  54.  
  55. 'got stuck here :(
  56.  
  57. DBAuser.SelectCommand.CommandText = "SELECT login,password FROM login WHERE userName = '" & txtUsername2.Text & "' AND password = '" & txtPassword2.Text & "'"
  58.  
  59. End Sub
  60.  
  61. End Class


Do i need some sort of Epic if statement to validate what the user inputted into the text boxes or am i on the wrong path?
Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Got stuck on creating a login and password form. Epic If statement needed

 
0
  #2
May 24th, 2009
Do i need some sort of Epic if statement to validate what the user inputted into the text boxes
I'm afraid you need it.
am i on the wrong path
I don't think so.

I don't know what type of DBAuser is. But the SQL syntax "SELECT login,password FROM login WHERE userName = '" & txtUsername2.Text & "' AND password = '" & txtPassword2.Text & "'" is correct. After you execute that SQL statement, just check that a one and only one record is returned. If you get nil records, either user name or/and the password is incorrect.

When you create a new account (INSERT statement), you should first check that the user name and the password combination does not exist already.

I would make a separate boolean function for that
  1. Private Function IsValidLogin(ByVal UserName As String, ByVal Password As String) As Boolean
  2. ' Check if UserName and Password are found in the DB and return True. Otherwise, return False
  3.  
  4. End Function
Put the DB code (SELECT statement and record count testing) in there and you can call it easily from both user login and when the user creates an account.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 300
Reputation: jireh is an unknown quantity at this point 
Solved Threads: 42
jireh's Avatar
jireh jireh is offline Offline
Posting Whiz

Re: Got stuck on creating a login and password form. Epic If statement needed

 
0
  #3
May 25th, 2009
add also in your login checking about sql injection or else it will be easily to hack...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: Got stuck on creating a login and password form. Epic If statement needed

 
0
  #4
May 25th, 2009
Jireh is right about SQL injection. I didn't mention it because it's rarely an issue with Windows apps. If you're going to use the code with ASP.NET, there's a really bad security hole. And you'll be hacked sooner or later.

You do some replaces in your SQL INSERT statement. A few characters that you shouldn't allow in user name and password are ";", "-" and "'". To be more precise, the correct way to do it in a "safe way", is to define a set of allowed characters (a-z, 0-9 and a few other printable characters). If the user tries to create an account with an user name and/or a password containing any character that is not an allowed character, it should be rejected.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 40
Reputation: lolwtf is an unknown quantity at this point 
Solved Threads: 0
lolwtf lolwtf is offline Offline
Light Poster

Re: Got stuck on creating a login and password form. Epic If statement needed

 
0
  #5
May 25th, 2009
DBAUser is my database adapter. As for the security and sql injection, this is just a personal project im doing for fun. I guess it would be useful later on so ill do some research on that. As for the password validation im still stuck on that part because I dont have much practice with Database adapters.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 40
Reputation: lolwtf is an unknown quantity at this point 
Solved Threads: 0
lolwtf lolwtf is offline Offline
Light Poster

Re: Got stuck on creating a login and password form. Epic If statement needed

 
0
  #6
May 26th, 2009
Okay i figured out the actual code if anybody's interested:

DBAuser.SelectCommand.CommandText = "SELECT COUNT(login) FROM login WHERE userName = '" & txtUsername2.Text & "' AND password = '" & txtPassword2.Text & "'"
DBAuser.Fill(DSLogin) 'fill dataset
If DSLogin.Tables(0).Rows(0).Item(0) > 0 Then '
'User entered proper login
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC