| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
Basic Login Validation
Here are some examples of validating a user login to an Access database.
You have a database containing usernames and passwords Code:
id username password 1 User1 Mypass 2 user2 12345 3 User3 abcd 4 Testuser1 pass123 5 Testuser2 abc123 You have a simple form for the user to enter their login details Code:
<form name="form1" method="post" action="">
<table width="200" border="1" cellspacing="0" cellpadding="5">
<tr>
<td>UserName</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
Here is some basic code to login the user 1) Check if the form has been submitted 2) Connect to the database 3) Query the database and create a recordset 4) Check if the recordset is empty or not 5) Output message to the user asp Code:
This will work but it has several problems. These include the lack of data validation, sanitization of data to prevent SQL Injection attacks and outputting relevant messages to the user.
__________________
CyberTechHelp |
|
#2
|
||||||||||||||||||||
|
||||||||||||||||||||
|
There are somethings that can be done before connecting to or querying the database.
The first is to validate the submitted data asp Code:
This is the part where the data is requested then validated asp Code:
Then we can output a simple message to the user if the user doesn't enter any data Quote:
To expand on this validation you can validate the variables seperately and construct the errMsg variable asp Code:
Quote:
If the user enters incorrect details then they will receive an error message asp Code:
Quote:
If the user enters correct details then they receive a welcome message ASP & HTML Code:
So the basic validation to make sure the user submits data is done. There is more validation to do though. |
|
#3
|
|||||
|
|||||
|
Data Sanitization - Prevent SQL Injection Attacks
The data has to also be sanitized to prevent SQL Injection attacks. At the moment the user submits their data it is fed directly into the SQL query. asp Code:
If the user enters username: user1 password: mypass then this is the executed query Quote:
To see the generated SQL query then uncommend the Response.Write rsSQL line Code:
'--------------------- '## DEBUG ## Response.Write rsSQL 'Response.End() '--------------------- At this point the user could user SQL Injection by using a single quotes/apostrophes in the form fields If the user enters username: ' or 1=1 or 'a' = 'a password: ' or 1=1 or 'a' = 'a then the executed query would be Quote:
This is a valid SQL query and if your output code was a loop to output the recordset then all the records would be output |