Exercise: Session variables for log-on

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend the course listed below!

Category ==> ASP.NET webforms - VB  (27 exercises)
Topic ==> Passing values between pages  (3 exercises)
Level ==> Harder than average
Course ==> ASP.NET WebForms - VB
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Create a new website, and add the files in the folder shown above into it.

Create a login form called frmLogon.aspx to allow users to log on to your website:

ASP.NET 4.0 - Visual Basic exercise - Passing values between pages (image 1)

The correct user name and password are shown filled in

If a user typed in the correct user name and password, the form should:

  • set the value of the session variable called UserId equal to 1; then
  • redirect you to the home page (frmHome.aspx).

Otherwise, the form should just display an error message and ask you to try logging in again.

Now attach code to your master page's Load event so that on first load the page checks for the existence of the session variable UserId.  Here's what should happen:

  • If this variable isn't set and this isn't the logon page, the system should redirect the user to the logon page; whereas
  • If the variable is set and this is the logon page, the system should redirect the user to the home page.

You can use Request.Url.AbsolutePath.ToLower.Contains to check whether the URL for the request includes the word frmlogon.

Check that your system works!  If you're ever doing this in anger yourself, a better way would be for each page apart from frmLogon.aspx to inherit from a different class (ask your trainer!).

This page has 0 threads Add post