A small addition to this code, which will allow the application to monitor the number of attempts at a login before granting or denying access.
a. Modify the Global.asax Session_Start method:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
'<summary>
' |Fires when the session is started
' |Administrator will only be allowed a certain number of login attempts
'</summary>
Session("Num_of_Tries") = 3
Session("LoginCount") = 0
' |Track whether they're logged in or not
Session("Logged_IN") = "No"
End Sub
b. Add the code for the button click event (in this case cmdSubmit button): -
Revised!
Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
If Page.IsValid Then ' ||||| Meaning the Control Validation was successful!
' ||||| Connect to Database for User Validation |||||
Dim intMaxLoginAttempts = CInt(Session("Num_of_Tries"))
If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
Session("Logged_IN") = "Yes" ' ||||| Use to Validate on other pages in the application
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False) ' ||||| default.aspx Page!
Else
' ||||| Credentials are Invalid
lblMessage.Text = "Invalid Login!"
' ||||| Increment the LoginCount (attempts)
Session("LoginCount") = CInt(Session("LoginCount")) + 1
' ||||| Determine the Number of Tries
If Session("LoginCount").Equals(intMaxLoginAttempts) Then
Response.Redirect("Denied.aspx")
End If
End If
End If
End Sub
c. Validate login on other pages in the application - Add to Page_Load Event
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' <summary>
' ||||| Authenicate user for accces to pages within application
' ||||| Enusre the page can't be navigated to without
' ||||| user's being online and logged in.
' ||||| **Note: Logged_IN session object is created in Session_Start
' ||||| of the Global.asax file **
' </summary>
' |Do not allow caching of page
Response.Cache.SetCacheability(HttpCacheability.NoCache)
If Session("Logged_IN").Equals("No") Then
Response.Redirect("Login.aspx")
End If
Happy coding

!