Solved

SQL Datareader only checks first line in database

Posted on 2007-11-15
7
414 Views
Last Modified: 2008-02-01
Hi

I am building a .net website and want to be able to use a login control to access a page where the username and password are taken from an existing database.

The code I have used is below:


Now this works great.... as long as you are the first person in the database table. For some reason the datareader doesn't read all the rows to match the username and password entered.

Any help would be greatly appreciated.
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs) 

    Dim Authenticated As Boolean = False 

    Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password) 

    e.Authenticated = Authenticated 

    If Authenticated = True Then 

        Response.Redirect("Home.aspx") 

    End If 

End Sub 
 

Private Function SiteLevelCustomAuthenticationMethod(ByVal UserName As String, ByVal Password As String) As Boolean 

    Dim boolReturnValue As Boolean = False 

    ' Insert code that implements a site-specific custom 

    ' authentication method here. 

    ' This example implementation always returns false. 

    Dim strConnection As String = "server=dtpxp-skumari;database=master;uid=sa;pwd=;" 

    Dim Connection As New SqlConnection(strConnection) 

    Dim strSQL As String = "Select * From Employee" 

    Dim command As New SqlCommand(strSQL, Connection) 

    Dim Dr As SqlDataReader 

    Connection.Open() 

    Dr = command.ExecuteReader() 

    While Dr.Read() 

        If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then 

            boolReturnValue = True 

        End If 

        Dr.Close() 

        Return boolReturnValue 

    End While 

End Function

Open in new window

0
Comment
Question by:DJFuller
7 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 83 total points
ID: 20288236
you close the datareader and return inside the while loop... bad move.
While Dr.Read()         

  If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then        

     boolReturnValue = True       

  End If        

End While 

Dr.Close()         

Return boolReturnValue     

Open in new window

0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 83 total points
ID: 20288239
Dr = command.ExecuteReader()    
While Dr.Read()        
   If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then
               boolReturnValue = True        
   End If
end while
Dr.Close()        
Return boolReturnValue
0
 
LVL 25

Accepted Solution

by:
imitchie earned 84 total points
ID: 20288347
if i may, break to speed things up

While Dr.Read()        
  If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then        
     boolReturnValue = True
     break
  End If        
End While
Dr.Close()        
Return boolReturnValue    
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 25

Expert Comment

by:imitchie
ID: 20288382
replace

    Dim strSQL As String = "Select * From Employee"
    Dim command As New SqlCommand(strSQL, Connection)
    Dim Dr As SqlDataReader
    Connection.Open()
    Dr = command.ExecuteReader()
    While Dr.Read()
        If (UserName = Dr("name").ToString()) And (Password = Dr("Password").ToString()) Then
            boolReturnValue = True
        End If
        Dr.Close()
        Return boolReturnValue
    End While

with

    Dim strSQL As String = "Select [name] From Employee where [name] = '" + Replace(UserName, "'", "''") + "' and [Password]= '" + Replace(Password, "'", "''") + "'"
    Dim command As New SqlCommand(strSQL, Connection)
    Dim Dr As SqlDataReader
    Connection.Open()
    Dr = command.ExecuteReader()
    boolReturnValue = Dr.Eof
    Dr.Close()
    Return boolReturnValue
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20288384
grr... time tosleep
    Dim strSQL As String = "Select [name] From Employee where [name] = '" + Replace(UserName, "'", "''") + "' and [Password]= '" + Replace(Password, "'", "''") + "'"

    Dim command As New SqlCommand(strSQL, Connection)

    Dim Dr As SqlDataReader

    Connection.Open()

    Dr = command.ExecuteReader()

    boolReturnValue = not Dr.Eof

    Dr.Close()

    Return boolReturnValue 

Open in new window

0
 
LVL 2

Author Comment

by:DJFuller
ID: 20288665
Oh my god.. what an idiot I am..

Thanks all of you... I guess I can share points out.

imitchie: what does adding break do?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20290034
>imitchie: what does adding break do?
it finishes the while immediately. as at that place, you knew the username/password is correct, no need to loop further in the DataReader.

the suggestion of imitchie to include the username/password test directly in the query is obviously the most efficient method, but could even be made better by using sqlcommand and sqlparameter objects ... but that is another discussion. just read up on those key terms will help you coding alot.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now