This is one using formsauthentication:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Sub Button_Click(ByVal s As Object, ByVal e As EventArgs)
If IsValid Then
If DBAuthenticate(txtUsername.Text, txtPassword.Text) > 0 Then
Web.Security.FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, False)
End If
End If
End Sub
Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
Dim conMyData As SqlConnection
Dim cmdSelect As SqlCommand
Dim intResult As Integer
conMyData = New SqlConnection(Application.Item("conn"))
cmdSelect = New SqlCommand("SELECT id, email, firstname, lastname, username, password, type, active FROM user_contact WHERE username = '" & strUsername & "' AND password = '" & strPassword & "'", conMyData)
Dim dr As SqlDataReader
conMyData.Open()
Dim pwd As String
dr = cmdSelect.ExecuteReader
If dr.Read Then
intResult = 1
Dim active As Boolean
If Not dr.IsDBNull(7) Then
active = dr.GetBoolean(7)
End If
If Not dr.IsDBNull(5) Then
pwd = dr.GetString(5)
End If
'this is here to check for case in the passwords
If pwd <> strPassword Then
dr.Close()
conMyData.Close()
conMyData.Dispose()
cmdSelect.Dispose()
intResult = -1
lbl_messege.Text = "Passwords do not match."
Exit Function
End If
If Not active Then
dr.Close()
conMyData.Close()
conMyData.Dispose()
cmdSelect.Dispose()
intResult = -1
lbl_messege.Text = "You account has been disabled. Please see your site administrator."
Exit Function
Else
Dim holdthis As String
If Not dr.IsDBNull(0) Then
Session("UID") = dr.GetInt32(0)
End If
If Not dr.IsDBNull(1) Then
Session("email") = dr.GetString(1)
End If
If Not dr.IsDBNull(2) Then
If Not dr.IsDBNull(3) Then
Session("name") = dr.GetString(2) & " " & dr.GetString(3)
holdthis = Session("name")
End If
End If
If Not dr.IsDBNull(4) Then
Session("username") = dr.GetString(4)
holdthis = Session("username")
End If
If Not dr.IsDBNull(6) Then
'later on will show information based on their level
Session("level") = dr.GetInt32(6)
End If
Session("password") = pwd
'good for 60 minutes of inactivity
Session.Timeout = 60
Dim sql As String
conMyData.Close()
conMyData.Open()
'insert into a user_login table to track when users login and out based on a session id
Dim sesid As String = HttpContext.Current.Session.SessionID
sql = "INSERT INTO user_login (uid, llogin, sessionid) VALUES (" & Session("uid") & ", '" & Now() & "', '" & sesid & "')"
cmdSelect = New SqlCommand(sql, conMyData)
cmdSelect.ExecuteNonQuery()
conMyData.Close()
conMyData.Dispose()
cmdSelect.Dispose()
End If
Else
intResult = -1
lbl_messege.Text = "Username Not Recognized"
End If
dr.Close()
conMyData.Close()
conMyData.Dispose()
cmdSelect.Dispose()
Return intResult
End Function
Brian Fairchild