Sport Girl
New member
- Joined
- Apr 18, 2008
- Messages
- 2
- Programming Experience
- Beginner
Hi Everybody,
Please can somebody help me with this little task:
i created a login form in windows form from the default template in VB.NET but the problem is that it is not functioning even with the correct corresponding code without any errors.
Well it is always prompting the messagebox "you must enter a username" whenever the situation is, besides the canccel button does not work in closing the form
Regards
Please can somebody help me with this little task:
i created a login form in windows form from the default template in VB.NET but the problem is that it is not functioning even with the correct corresponding code without any errors.
Well it is always prompting the messagebox "you must enter a username" whenever the situation is, besides the canccel button does not work in closing the form
VB.NET:
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports System.Security
Imports System.Security.Cryptography
Public Class LoginForm
Inherits System.Windows.Forms.Form
' TODO: Insert code to perform custom authentication using the provided username and password
' (See [url]http://go.microsoft.com/fwlink/?LinkId=35339[/url]).
' The custom principal can then be attached to the current thread's principal as follows:
' My.User.CurrentPrincipal = CustomPrincipal
' where CustomPrincipal is the IPrincipal implementation used to perform authentication.
' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
' such as the username, display name, etc.
Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitializeComponent()
End Sub
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim AuthenticationReader As System.Data.SqlClient.SqlDataReader
'Intitalizes connection object
Dim SqlConnection As New SqlClient.SqlConnection
SqlConnection.ConnectionString = "Server=owner;database=***"
'Initializes command object to execute statements.
Dim AuthenticationCommand As New SqlClient.SqlCommand
AuthenticationCommand.Connection = SqlConnection
'Declarations
Dim DBusername As String
Dim DBuserpassword
Dim DBuserID As Integer
Dim DBlogggedname As String
Dim DBprivilege As String
If UsernameTextBox.Text.Length = 0 Then
MessageBox.Show("You must enter a username", "Invalid Username", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
UsernameTextBox.Focus()
ElseIf PasswordTextBox.Text.Length = 0 Then
MessageBox.Show("You must enter a password", "Invalid Password", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
PasswordTextBox.Focus()
Else
DBusername = Me.UsernameTextBox.Text
DBuserpassword = Me.PasswordTextBox.Text
'Executes command
Dim AuthenticationString As String
AuthenticationString = "select * from *** where UserName= '" & UsernameTextBox.Text & "'"
AuthenticationCommand.CommandText = AuthenticationString
SqlConnection.Open()
AuthenticationCommand.ExecuteNonQuery()
'Encrypt Password
Dim md5hasher As New MD5CryptoServiceProvider
Dim hashedBytes As Byte()
Dim encoder As New System.Text.UTF8Encoding
hashedBytes = md5hasher.ComputeHash(encoder.GetBytes(PasswordTex tBox.Text))
AuthenticationReader = AuthenticationCommand.ExecuteReader()
If AuthenticationReader.HasRows Then
While (AuthenticationReader.Read())
DBusername = AuthenticationReader("UserName")
DBuserpassword = AuthenticationReader("Password")
DBuserID = AuthenticationReader("UserID")
DBlogggedname = AuthenticationReader("Name")
DBprivilege = Trim(AuthenticationReader("Privilege"))
End While
Else
MessageBox.Show("Invalid username!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
UsernameTextBox.Focus()
Exit Sub
End If
AuthenticationReader.Close()
SqlConnection.Close()
If bHashEquals(hashedBytes, DBuserpassword) Then
Dim Frame As New Frame()
Frame.Show()
Me.Hide()
Else
MessageBox.Show("Invalid password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
PasswordTextBox.Focus()
End If
End If
End Sub
Private Function bHashEquals(ByVal byHash1 As Byte(), ByVal byHash2() As Byte) As Boolean
bHashEquals = False
If byHash1.Length = byHash2.Length Then
Dim i As Integer
Do While (i < byHash1.Length) AndAlso (byHash1(i) = byHash2(i))
i = i + 1
Loop
If i = byHash1.Length Then
bHashEquals = True
End If
End If
End Function
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
End Class
Regards