Login - Username and Password

Ok, this is a similiar sample that I posted earlier, Login. It keeps saying the password is invalid. Username is not changed. It does not redirect to a "default.aspx". I checked Query Analyzer and verified username and password.



Imports System.Data.SqlClient

Partial Class Login
    Inherits System.Web.UI.Page

 
 

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

        Dim Authen As Boolean = False
        Authen = MyCustAuthen(Login1.UserName, Login1.Password)
        e.Authenticated = Authen
        If Authen = True Then
            Response.Redirect("default.aspx")

        End If
    End Sub
    Private Function MyCustAuthen(ByVal UserName As String, ByVal Password As String) As Boolean

       

        Dim BoolReturnValue As Boolean = False
        Dim StrCon As String = "Data Source=mcci\superman;Initial Catalog=Login;Persist Security Info=True;User ID=myusername;Password=mypassword"
        Dim Con As SqlConnection = New SqlConnection(StrCon)
        Dim Strselect As String = "select * from Loginnow"
        Dim myCommand As SqlCommand = New SqlCommand(Strselect, Con)
        Dim Dr As SqlDataReader


        Try

            Con.Open()

            Dr = myCommand.ExecuteReader


            While Dr.Read
                If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
                    BoolReturnValue = True
                End If
                Dr.Close()

                Return BoolReturnValue


            End While



        Catch ex As Exception

            MsgBox(ex.ToString)


        End Try
       

    End Function


End Class
VBdotnet2005Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Hamed ZaghaghiConnect With a Mentor ProgrammerCommented:
did you check the MyCustAuthen line by line, and check why with the correct username and password, it returns false?
0
 
Hamed ZaghaghiProgrammerCommented:
what is wrong here?

do you want to redirect to default.aspx, and this code didn't do that?
0
 
VBdotnet2005Author Commented:
No. It did not redirect to default.aspx. It keeps saying the login is invalid. The textbox"Username" it did not clear out. It stays to the value whatever I input, lets say, Jack and it password is the only one I have to enter.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
VBdotnet2005Author Commented:
Ok here is the username : Jack and the password is : SParrow
0
 
Hamed ZaghaghiProgrammerCommented:
so you want to do this:
after the user login incorrectly, the login control, keep the user name and clear the password textbox, so the user can input only the password?


0
 
VBdotnet2005Author Commented:
Exectly, that's what it does. I can't tell wheter the username or the password is incorrect.
0
 
Hamed ZaghaghiProgrammerCommented:
did you use failurText property of Login1
0
 
Hamed ZaghaghiProgrammerCommented:
or you can convert the login control to webtemplate, so you have access to all controls on it, and also access to failur label, and you can set it visible and invisible

0
 
Hamed ZaghaghiProgrammerCommented:
I set the failur text, and then in  Login1_Authenticate set

e.Authenticated = false;

so the login control show the failur text,

if you use it, what is wrong?
0
 
VBdotnet2005Author Commented:
This line


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


"name" and "passwordx"  are supposed to be the same as Column Name in Enterprise Manager, right?
0
 
VBdotnet2005Author Commented:
I set a break point here

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

received this error

error -       System.IndexOutOfRangeException      {"Password"}      System.IndexOutOfRangeException
0
 
Hamed ZaghaghiProgrammerCommented:
do you write correct field name?

passwordx or password?

check it by your table in enterprise manager, character by character.
0
 
VBdotnet2005Author Commented:
What does it means?

Item = In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
0
 
VBdotnet2005Author Commented:
in Enterprise manager, I put this

Column name

passwordx  

data type

Char

lenght

10

Allow Nulls is checked
0
 
VBdotnet2005Author Commented:
Loginnow Table I have 3 columns

Id - set as primary key , increment by 1
name
passwordx
0
 
Hamed ZaghaghiProgrammerCommented:
ok,

let use int indexes instead of string indexes: so you want to change

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

to

If (UserName = Dr(1).ToString And Password = Dr(2).ToString) Then
0
 
VBdotnet2005Author Commented:

I have tried that, but it does not work.

If (UserName = Dr(1).ToString And Password = Dr(2).ToString) Then


...


I set a break point here

If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
or your code


If (UserName = Dr(1).ToString And Password = Dr(2).ToString) Then


under Autos   +++

Dr(1) ---- value is "John"  type Object   ---------------John is the first name in Loginnow, this is definately wrong
string is "John"  

Password is "sparrow"
Username is "Jack"




0
 
Hamed ZaghaghiProgrammerCommented:
if you have a table like this:

col1, col2, col3, col4, col5

and you want to use 4th column of your table should use Dr(3),

so if the username filed in your table is 3th field change the Dr(1) to Dr(2),

indexes strat from 0.
0
 
VBdotnet2005Author Commented:
Of course, this is what I used.

Jack Sparrow is #6 in Query Analyzer

Password is "sparrow"
Username is "Jack"
0
 
VBdotnet2005Author Commented:
I do understand is part.

"so if the username filed in your table is 3th field change the Dr(1) to Dr(2),

indexes strat from 0."
0
 
Hamed ZaghaghiProgrammerCommented:
what's wrong now?
0
 
VBdotnet2005Author Commented:
What you think about this one?

Dr(1) ---- value is "John"  type Object   ---------------John is the first name (ID #1) in Loginnow, this is definately wrong
string is "John"  

This is what I just typed in.

Password is "sparrow"
Username is "Jack"
0
 
VBdotnet2005Author Commented:

It labled this

Your login attempt was not successful. Please try again.

It does not redirect to "default.aspx"
0
 
Hamed ZaghaghiProgrammerCommented:
i find the bug:

when u use this sql command

Dim Strselect As String = "select * from Loginnow"

then it return all information in Loginnow table, and beacuase you check only one of return values, can't find the username you enter,

so you have to way to correct that:

1: change the sql command string like this:

Dim Strselect As String = "select * from Loginnow where name='" & UserName &"' and passwords='" & Password & "' "


2:
change th while loop to check all values returned:

While Dr.Read
                If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
                    BoolReturnValue = True
                End If
End While

Dr.Close()

Return BoolReturnValue
0
 
Hamed ZaghaghiProgrammerCommented:
use failure action property
0
 
VBdotnet2005Author Commented:

Dim Strselect As String = "select * from Loginnow where name='" & UserName &"' and passwords='" & Password & "' "

supposed to be

Dim Strselect As String = "select * from Loginnow where name='" & UserName &"' and passwordx='" & Password & "' "

this is changed

While Dr.Read
                If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
                    BoolReturnValue = True
                End If
End While

...

still


Your login attempt was not successful. Please try again.

It does not redirect to "default.aspx"
0
 
Hamed ZaghaghiProgrammerCommented:
Ooooooops also change this:

            While Dr.Read
                If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
                    BoolReturnValue = True
                    Dr.Close()
                    Return BoolReturnValue
                End If
            End While
            Dr.Close()
            Return BoolReturnValue

0
 
VBdotnet2005Author Commented:
The entire code

-------------------


Imports System.Data.SqlClient

Partial Class Login
    Inherits System.Web.UI.Page

 
 

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

        Dim Authen As Boolean = False
        Authen = MyCustAuthen(Login1.UserName, Login1.Password)
        e.Authenticated = False
        If Authen = True Then
            Response.Redirect("default.aspx")

        End If
    End Sub
    Private Function MyCustAuthen(ByVal UserName As String, ByVal Password As String) As Boolean

       

        Dim BoolReturnValue As Boolean = False
        Dim StrCon As String = "Data Source=MCCI\superman;Initial Catalog=Login;Persist Security Info=True;User ID=myusernamePassword=password"
        Dim Con As SqlConnection = New SqlConnection(StrCon)

        Dim Strselect As String = "select * from Loginnow where name='" & UserName & "' and passwordx='" & Password & "' "
        Dim myCommand As SqlCommand = New SqlCommand(Strselect, Con)
        Dim Dr As SqlDataReader


        Try

            Con.Open()

            Dr = myCommand.ExecuteReader

           
            While Dr.Read
                If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
                    BoolReturnValue = True
                End If
                Dr.Close()

                Return BoolReturnValue


            End While




        Catch ex As Exception

            MsgBox(ex.ToString)


        End Try
       

    End Function


End Class
0
 
VBdotnet2005Author Commented:
still does not work  -   I am about to give up


Your login attempt was not successful. Please try again.

It does not redirect to "default.aspx"
0
 
VBdotnet2005Author Commented:


Anything else you can think of. I will continue it tomorrow. I'm off to bed now. Thank you for your help.
0
 
Hamed ZaghaghiProgrammerCommented:
when you set this:

e.Authenticated = False

then you want that code works properly,
you should change it to:

e.Authenticated = Authen
0
 
VBdotnet2005Author Commented:
The entire code  - still

Your login attempt was not successful. Please try again.

It does not redirect to "default.aspx"

Tomorrow, I will create a brand new table and a brand new asp.net project and see if that help(with the same code)

-------------------


Imports System.Data.SqlClient

Partial Class Login
    Inherits System.Web.UI.Page

 
 

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

        Dim Authen As Boolean = False
        Authen = MyCustAuthen(Login1.UserName, Login1.Password)
        e.Authenticated = Authen
        If Authen = True Then
            Response.Redirect("default.aspx")

        End If
    End Sub
    Private Function MyCustAuthen(ByVal UserName As String, ByVal Password As String) As Boolean

       

        Dim BoolReturnValue As Boolean = False
        Dim StrCon As String = "Data Source=MCCI\superman;Initial Catalog=Login;Persist Security Info=True;User ID=myusernamePassword=password"
        Dim Con As SqlConnection = New SqlConnection(StrCon)

        Dim Strselect As String = "select * from Loginnow where name='" & UserName & "' and passwordx='" & Password & "' "
        Dim myCommand As SqlCommand = New SqlCommand(Strselect, Con)
        Dim Dr As SqlDataReader


        Try

            Con.Open()

            Dr = myCommand.ExecuteReader

           
            While Dr.Read
                If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
                    BoolReturnValue = True
                End If
                Dr.Close()

                Return BoolReturnValue


            End While




        Catch ex As Exception

            MsgBox(ex.ToString)


        End Try
       

    End Function


End Class
0
 
Hamed ZaghaghiProgrammerCommented:
check with this:

Imports System.Data.SqlClient

Partial Class Login
    Inherits System.Web.UI.Page

 
 

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

        Dim Authen As Boolean = False
        Authen = MyCustAuthen(Login1.UserName, Login1.Password)
        e.Authenticated = Authen
        If Authen = True Then
            Response.Redirect("default.aspx")

        End If
    End Sub
    Private Function MyCustAuthen(ByVal UserName As String, ByVal Password As String) As Boolean

       

        Dim BoolReturnValue As Boolean = False
        Dim StrCon As String = "Data Source=MCCI\superman;Initial Catalog=Login;Persist Security Info=True;User ID=myusername;Password=password"
        Dim Con As SqlConnection = New SqlConnection(StrCon)

        Dim Strselect As String = "select * from Loginnow where name='" & UserName & "' and passwordx='" & Password & "' "
        Dim myCommand As SqlCommand = New SqlCommand(Strselect, Con)
        Dim Dr As SqlDataReader


        Try

            Con.Open()

            Dr = myCommand.ExecuteReader

           
            While Dr.Read
                If (UserName = Dr("name").ToString And Password = Dr("passwordx").ToString) Then
                    BoolReturnValue = True
                    Dr.Close()
                    Return BoolReturnValue
                End If
            End While

            Dr.Close()

            Return BoolReturnValue




        Catch ex As Exception

            MsgBox(ex.ToString)


        End Try
       

    End Function


End Class
0
 
VBdotnet2005Author Commented:
Did you changed anything? I can't tell.
0
 
VBdotnet2005Author Commented:
I tried to creat a new table, but still give me the same result. There shouldn't be any errors there, right?

Your login attempt was not successful. Please try again.

It does not redirect to "default.aspx"
0
 
VBdotnet2005Author Commented:
How about Login1 properties? Should I change anything there?
0
 
VBdotnet2005Author Commented:
oK. I created a brand new database name "Loginonweb" and table name is "Logmein" so we will get confused with Name and password or passwordx
It still gives me the same result

Your login attempt was not successful. Please try again.

It does not redirect to "default.aspx"




cloumn name   data type   lenght allow nulls
id                     int            4         unchecked
cid                   char          10       checked
zipcode            numeric     9         checked




Imports System.Data.SqlClient

Partial Class Login
    Inherits System.Web.UI.Page

 
 

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate

        Dim Authen As Boolean = False
        Authen = MyCustAuthen(Login1.UserName, Login1.Password)
        e.Authenticated = Authen
        If Authen = True Then
            Response.Redirect("default.aspx")

        End If
    End Sub
    Private Function MyCustAuthen(ByVal UserName As String, ByVal Password As String) As Boolean

       

        Dim BoolReturnValue As Boolean = False
        Dim StrCon As String = "Data Source=MCCI\superman;Initial Catalog=Loginonweb;Persist Security Info=True;User ID=ashley;Password=password"
        Dim Con As SqlConnection = New SqlConnection(StrCon)

        Dim Strselect As String = "select *from Logmein where cid = '" & UserName & "' and zipcode ='" & Password & "' "
        Dim myCommand As SqlCommand = New SqlCommand(Strselect, Con)
        Dim Dr As SqlDataReader


        Try

            Con.Open()

            Dr = myCommand.ExecuteReader

           
            While Dr.Read
                If (UserName = Dr("cid").ToString And Password = Dr("zipcode").ToString) Then
                    BoolReturnValue = True
                    Dr.Close()
                    Return BoolReturnValue
                End If
            End While
            Dr.Close()
            Return BoolReturnValue




        Catch ex As Exception

            MsgBox(ex.ToString)


        End Try
       

    End Function


End Class
0
 
VBdotnet2005Author Commented:
Yes, I did. Username = Z16 and password = 93232 and that is what I have on the table. They matched,but it still returns false, which I just don't get it.
0
 
VBdotnet2005Author Commented:

Have you thought of anything else?
0
All Courses

From novice to tech pro — start learning today.