Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 21st, 2004, 04:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default Form Authentication

I'm trying to run through the database table and match txtUserName and txtPassword to values currently in the database. If they match redirect them to the members only page. If the password doesn't match display "Invalid UserName or Password!". If the username is not found then redirect them to the register page.

The problem is when the username doesn't exist it does nothing when it's supposed to redirect to the register page.

Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
        Dim intResult As Integer

        Dim strConn As String = ConfigurationSettings.AppSettings("membs")
        Dim objConn As New OleDbConnection(strConn)
Dim strSQL As String = "SELECT password, username FROM members" & _
            "WHERE (password= '" & strUsername & "');"

        Dim objAdapter As New OleDbDataAdapter(strSQL, objConn)
        Dim objDataSet As New DataSet

        objAdapter.Fill(objDataSet, "dtMembers")
        objConn.Close()

        Dim r As DataRow
        For Each r In objDataSet.Tables("dtNews").Rows
            If r("password") = strPassword And r("username") = strUsername Then
                intResult = 1
            Else
                intResult = -1
            End If
        Next
        If intResult < 0 Then
            lblMessage.Text = "Invalid UserName or Password!"
        End If
        Return intResult
    End Function



 
Old January 21st, 2004, 04:56 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Where's your Response.Redirect()?

<sidenote>
Many people recommend not telling the user that the password is bad (explicitly or implicitly telling them that the username is good). This encourages attempts to guess the password for a known good users. Usually you'd display an error like "No valid login for the username and password combination you entered could not be found." This provides useful but not disclosing feedback.
</sidenote>

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 21st, 2004, 04:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I haven't added it in there yet. I was trying to get it to work first by displaying Invalid Username or Password.

 
Old January 21st, 2004, 05:01 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Ok, so what's the question then?

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 21st, 2004, 05:03 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Oh, I just noticed something....

Dim strSQL As String = "SELECT password, username FROM members" & _
            "WHERE (password= '" & strUsername & "');"

shouldn't this be the username field?

Dim strSQL As String = "SELECT password, username FROM members" & _
            "WHERE (username= '" & strUsername & "');"
 
Old January 21st, 2004, 05:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

The problem is when the username doesn't exist it does nothing when it's supposed to redirect to the register page.

It should as of now display Invalid UserName or Password if no username can be found.

 
Old January 21st, 2004, 05:04 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well, how can it redirect if you aren't telling it to redirect?
 
Old January 21st, 2004, 05:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
quote:Originally posted by planoie
 Oh, I just noticed something....

Dim strSQL As String = "SELECT password, username FROM members" & _
            "WHERE (password= '" & strUsername & "');"

shouldn't this be the username field?

Dim strSQL As String = "SELECT password, username FROM members" & _
            "WHERE (username= '" & strUsername & "');"
Sorry that was a typo. It reads the way you pointed out.
 
Old January 21st, 2004, 05:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Should I have 2 sql statements and datasets to check for password in one and username in the other?

 
Old January 21st, 2004, 05:17 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

How about this...

Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Boolean
    Dim bResult As Boolean = False
    Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings( "membs"))
    Dim strSQL As String
    Dim strGoodPassword As String
    Dim objCommand As New OleDbCommand

    objCommand.Connection = objConn
    strSQL = String.Format("SELECT password FROM members WHERE (username='{0}');", strUsername)
    objCommand.CommandText = strSQL
    objCommand.CommandType = CommandType.Text

    objConn.Open()
    strGoodPassword = CType(objCommand.ExecuteScalar, String)
    objConn.Close()

    If Not strGoodPassword Is Nothing Then
        'Good user
        If strGoodPassword = strPassword Then
            bResult = True
        Else
            'bad password
            lblMessage.Text = "Invalid UserName or Password!"
        End If
    Else
        'No user, redirect to registration page
        Response.Redirect("register.aspx", False)
    End If

    Return bResult
End Function

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Form Authentication aadz5 J2EE 1 November 24th, 2006 08:09 AM
Form Authentication petrons BOOK: Professional Apache Tomcat 1 October 31st, 2006 06:41 AM
Form based authentication.... avanishp General .NET 2 June 17th, 2005 03:11 AM
Form Authentication Help (IIS) [email protected] VS.NET 2002/2003 5 July 27th, 2004 02:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.