Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,031 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,601 people online right now. Registration is fast and FREE... Join Now!




Validating Username & Password from Form

 
Reply to this topicStart new topic

Validating Username & Password from Form

svital
5 Mar, 2008 - 01:50 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 2

I do not know coding, but I am looking for code to use in my code behind page that will validate input of a username and password? Can someone point me in the right direction?

Thanks!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Validating Username & Password From Form
5 Mar, 2008 - 02:42 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks smile.gif
User is offlineProfile CardPM
+Quote Post

Semple
RE: Validating Username & Password From Form
5 Mar, 2008 - 03:27 PM
Post #3

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 12


My Contributions
Heres one that ive used before

CODE
Private Sub btnLogIn_Click(...) Handles btnLogIn.Click
    Dim Password, InputPassword As String
    Dim Attempt As Integer
    Password = "enter"                                                     'initiialise variables
    Attempt = 0

   Do                                                                               'start of loop
        Attempt = Attempt + 1
        InputPassword = InputBox("Entr password. This is attempt " & "number                       " & Attempt)

    Loop Until (Attempt = 3) Or (InputPassword = Password) 'end of loop
    If InputPassword = Password Then
      MsgBox  ("This password is valid")
    Else
      MsgBox  ("This password is invalid")
    End If
End Sub


Its not amazing but it gives you the basic outline of how it works.

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Validating Username & Password From Form
5 Mar, 2008 - 03:31 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
@Semple, we have a policy of not just handing out code because someone asks for it. They need to put forth the effort and show what they have attempted
User is offlineProfile CardPM
+Quote Post

Semple
RE: Validating Username & Password From Form
5 Mar, 2008 - 03:42 PM
Post #5

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 12


My Contributions
Sorry, but i wouldnt say the code is very good, just a push in the right direction.

If he doesnt understand coding, then he wouldnt have a clue, i could have just typed out the alphabet.
User is offlineProfile CardPM
+Quote Post

svital
RE: Validating Username & Password From Form
6 Mar, 2008 - 04:55 AM
Post #6

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 2

OK, this is what I have. It does not work. It really seems to break when I include the AND part on the SQL select statement.

CODE


        Dim myReader As Data.SqlClient.SqlDataReader

        Dim mySqlConnection As Data.SqlClient.SqlConnection

        Dim mySqlCommand As Data.SqlClient.SqlCommand



        mySqlConnection = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("svita076ConnectionString").ToString())

        Dim sql As String = "SELECT UserLogonID, UserPassword FROM MyUsers WHERE UserLogonID = '" & Me.userid.Text & "' AND Userpassword = '" & Me.psword.Text & "'"



        mySqlCommand = New Data.SqlClient.SqlCommand(sql, mySqlConnection)



        Try

            mySqlConnection.Open()

            myReader = mySqlCommand.ExecuteReader()



            If (myReader.HasRows) Then

                'Read in the first row to initialize the DataReader; we will on read the first row.

                myReader.Read()

                Dim content As ContentPlaceHolder

                content = Page.Master.FindControl("main")

                Dim lbl As New Label()

                lbl.Text = "<br/><br/>Logon Successful<br/>Welcome, " & myReader("UserFullName")


                content.Controls.Add(lbl)

            Else
                Dim content As ContentPlaceHolder

                content = Page.Master.FindControl("main")

                Dim lbl1 As New Label()

                lbl1.Text = "<br/><br/>Logon Failure<br/>Try Again"

                content.Controls.Add(lbl1)

            End If

        Catch ex As Exception

            Console.WriteLine(ex.ToString())

        Finally

            If Not (myReader Is Nothing) Then

                myReader.Close()

            End If

            If (mySqlConnection.State = Data.ConnectionState.Open) Then

                mySqlConnection.Close()

            End If

        End Try



Thanks, Sondra

This post has been edited by svital: 6 Mar, 2008 - 05:23 AM
User is offlineProfile CardPM
+Quote Post

nofear217
RE: Validating Username & Password From Form
31 Mar, 2008 - 06:56 AM
Post #7

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
In your select statement you have
CODE
Dim sql As String = "SELECT UserLogonID, UserPassword FROM MyUsers WHERE UserLogonID = '" & Me.userid.Text & "' AND Userpassword = '" & Me.psword.Text & "'"


I typically would use something like
CODE
SELECT Count(*) from MyUsers WHERE UserLogonID = '" & me.userid.Text & "' AND Userpassword = '" me.psword.Text & "'"

And also, there's no reason to use a reader....just do an execute scalar and then you can test if the value is 0 or not...if it's not then the username and password combination exist...something like
CODE
con.Open()
                Dim cmd As SqlCommand = New SqlCommand
                cmd.CommandText = "select count(*) from logins where userNames ='" & _
                me.userid.Text & "' and passwords = '" & hash & "'"
                cmd.Connection = con
                Dim i As String = cmd.ExecuteScalar

                If i = "0" Then
                    MsgBox("Unknown Username and/or Password")
                    txtUsername.Text = ""
                    txtOldPassword.Text = ""
                    txtNewPassword.Text = ""
                    txtRePassword.Text = ""
                Else


However, I would recommend not storing plain text passwords in your database anyway. You could use something like the following function to hash your passwords
CODE
' Hash the password string and return it as a 32 character hex string
    Protected Friend Function getMd5Hash(ByVal input As String) As String

        Dim md5Hasher As New MD5CryptoServiceProvider()

        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))

        Dim sBuilder As New StringBuilder()

        For i As Integer = 0 To data.Length - 1

            sBuilder.Append(data(i).ToString("x2"))

        Next i

        Return sBuilder.ToString()

    End Function
Then you can store the passwords as a hash....hash it before you query the database....that way the only thing that ever gets transmitted or read would be an MD5 hash rather than plain text. All you have to do in code is something like
CODE
dim source as string = salting + me.psword.Text
dim hash as string = getMd5Hash(source)


p.s. the salting is a salt string that I declare elsewhere that adds a bit more security to the password

This post has been edited by nofear217: 31 Mar, 2008 - 07:01 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Validating Username & Password From Form
31 Mar, 2008 - 07:07 AM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
What is the error message you're receiving?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month