Login.aspx [ form page ]
Login.asp.vb [code behind]
Imports System.IO
Imports System.Data.OleDb
Imports System.Web.UI.WebControls.ImageButton
Imports System.Web.UI.WebControls.TextBox
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web
Partial Class Control_Panel
Inherits System.Web.UI.Page
Protected Sub Login_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Login.Click
‘/ defining controls
Dim usn As TextBox
Dim pwd As TextBox
‘/ binding controls
usn = Login.FindControl(“usn”)
pwd = Login.FindControl(“pwd”)
Dim user As String
Dim key As String
user = usn.Text.ToString()
key = pwd.Text.ToString()
‘/ password encryption [SHA1]
Dim sha1Obj As New System.Security.Cryptography.SHA1CryptoServiceProvider
Dim bytesToHash() As Byte= System.Text.Encoding.ASCII.GetBytes(key)
bytesToHash = sha1Obj.ComputeHash(bytesToHash)
Dim keyCode As String = “”
For Each b As Byte In bytesToHash
keyCode += b.ToString(“x2”)
Next
‘/ access db connection – MS Access 2007/10 driver * you can define your own sql connection
Dim ConnDB As New OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;DataSource=” & Server.MapPath(“my_user.accdb”))
Dim cmd As New OleDbCommand(“SELECT * FROM users WHERE (usn = ‘” &usn.Text.ToString() & “‘) AND (pwd = ‘” & keyCode & “‘);”,ConnDB)
Dim reader As OleDbDataReader
Try
‘/ open connection
cmd.Connection.Open()
reader = cmd.ExecuteReader()
If reader.Read() Then
LabelUSN.Text = “<font color=’green’>Login successful!</font> – <a href=’Control-Panel.aspx?do=1′>Enter</a> secure pages”
Session(“Name”) = reader(1).ToString()
Else
Label2.Text = (“<font color=’red’>Login failed</font>”)
End If
‘/ close connection
cmd.Connection.Close()
Catch ex As Exception
Label2.Text = “<font color=’red’>Error:</font> “ & ex.Message.ToString()
End Try
End Sub
End Class