login.aspxReturn to the Documentation page
  

functionsPage_Load
Login

<%@ Page Language="VB" %>

<script runat="server">

‘The login.aspx file is loaded automatically on the client's machine if the user requests a restricted file, typically one of the four files mentioned below. These four files are denied to anonymous users by the location sections of the application's web.config file, which will load the login.aspx file instead. It's only after the user has successfully logged in will the requested file be displayed on the user's computer.

The Page_Load function first determines the referring Url, i.e. the page from which the login.aspx page had been called from - either the main menu page or the by-elections page. The Hyperlink control [linkToEdit] is modified accordingly.’

‘Declare a variable of type string [fileName] which will hold the referring url - something like 'http://www.honkywallah.me.uk/election2005/mainmenu.aspx'.’

dim fileName as string = Request.UrlReferrer.ToString()

‘Starts an IF...THEN...ELSE...END IF block which checks to see if the string 'mainmenu' is contained within the referring url or 'fileName'.’

if InStr(1,fileName,"mainmenu") <> 0 then

‘If it is, then set the Text property of the HyperLink control [linkToEdit] to 'Return to the Main menu'...’

linkToEdit.Text = "Return to the Main menu"

‘...and set the NavigateUrl property to 'mainmenu.aspx' so that a click by the user on the control will take them back to the Main menu.’

linkToEdit.NavigateUrl = "mainmenu.aspx"

‘However, if the string 'mainmenu' is NOT present within the referring url then....’

Else

‘...set the Text property of the HyperLink control [linkToEdit] to 'Return to the By-elections menu'...’

linkToEdit.Text = "Return to the By-elections menu"

‘...and finally set the NavigateUrl property to 'byelections.aspx' so that a click by the user on the control will take them back to the By-elections menu.’

linkToEdit.NavigateUrl = "byelections.aspx"

‘Ends the IF...THEN...ELSE...END IF block.’

End If

‘The Login function is run when the user clicks the Button control [btnSubmit] at the bottom of the form. It allows a user to gain access to certain restricted files providing they can enter a valid username and password.’

‘The username entered into the textbox [txtUserName] will be assigned to a variable of type string [user].’

Dim user as string = txtUserName.Text

‘Similarly, the password entered into the textbox [txtPassword] will be assigned to the string variable [pass].’

Dim pass as string = txtPassword.Text

‘Starts an IF...THEN...ELSE...END IF block which uses the Authenticate method of the FormsAuthentication object to check whether the data entered is valid...’

If FormsAuthentication.Authenticate(user, pass) Then

‘If the data is valid then the RedirectFromLoginPage method will send the user to the page originally requested. This could be either the updateanMP.aspx, updateaByelection.aspx, addaByelection.aspx or deleteaByelection.aspx page. The data entered by the user will be validated against the credentials section of the web.config file.

In addition to redirecting the user to the desired page, the RedirectFromLoginPage method also creates an authentication cookie [election2005cookie=txtUserName] which will authenticate the user until they sign out or close the browser.’

FormsAuthentication.RedirectFromLoginPage(txtUserName.Text,false)

‘However, if the data entered by the user is NOT valid i.e. either the username or password is incorrect then...’

Else

‘...set the Text property of the Label control [lblErrorLabel] to the appropriate message.’

lblErrorLabel.Text = "<b><font color='red'>Please enter a valid username and password</font></b>"

‘Ends the IF...THEN...ELSE...END IF block.’

End If

<html>

<head>

<title>Login form...</title>

<link href=".\otherfiles\election2005.css" type="text/css" rel="stylesheet" />

</head>

‘The Hyperlink control below [linkToEdit] has two of its properties, NavigateUrl and Text, set at runtime - see the Page_Load function above. This will allow the user to click the control and return back to the page they had come from if they so wish. Finally two textboxes [txtUserName and txtPassword], a button [btnSubmit] and a label [lblErrorLabel] make up the login form.’

<body bgcolor="white">

<form id="form1" runat="server">

<table width="100%">

<tr><td rowspan="2" width="85px"><img src="./images/cross.gif" title="Make voting compulsory!"></td><td><asp:Label id="Label1" runat="server"><u>Login</u></asp:Label></td><td align="right"><asp:HyperLink id="linkToEdit" runat="server"></asp:HyperLink></td></tr>

<tr><td colspan="2"> </td></tr>

</table><p>

<center>

<Please enter your username and password:<p>>

<div style="BORDER-RIGHT: black 2px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 2px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 2px solid; WIDTH: 350px; PADDING-TOP: 5px; BORDER-BOTTOM: black 2px solid; BACKGROUND-COLOR: #c0c0c0">

<asp:Label id="lblErrorLabel" runat="server" /><P><br>

<Username: <asp:Textbox id="txtUserName" runat="server" width="100" /><br>

<Password: <asp:Textbox id="txtPassword" runat="server" width="100" TextMode="password" style="position:relative;left:1px" /><p>

<asp:Button id="btnSubmit" runat="server" onclick="Login" Text="Login" /><p></div>

</center>

</form>

</body>

</html>

***********************************************