AMIT3023 Web Application Programming Practical Exercise 7
AMIT3023 Web Application Programming Practical Exercise 7
Practical Exercise 7
• Create a new SQL Server Express database named [UserDB.mdf] in the [App_Data] folder.
• Create a new database view named [User] that extract login data from both tables. The SQL
required is as follows:
• Right-click the [User] view and select [Show Results] to examine its data:
• Add a new DBML (LINQ to SQL Classes) file named [UserDB.dbml] to the project.
• From Server Explorer, drag-and-drop the [Member] table, the [Admin] table and [User]
view to the Object Relational (OR) Designer. The following entity classes should be
generated:
• In Solution Explorer, open the [Web.config] file under the root folder.
• Add a new <authentication> element under the hierarchy [configuration > system.web].
Type the XML codes as shown below:
We are implementing
Forms Authentication
• Within the same [Web.config] file, add a new <location> element under the hierarchy
[configuration]. Write authorization rules to prevent Anonymous users from accessing the
[Protected.aspx] page:
• Create a new [Web.config] file under the [MemberOnly] folder. Add a new <authorization>
element under the hierarchy [configuration > system.web]. Write authorization rules to
allow ONLY Member users to access the folder:
• Create a new [Web.config] file under the [AdminOnly] folder. Add a new <authorization>
element under the hierarchy [configuration > system.web]. Write authorization rules to
allow ONLY Admin users to access the folder:
• Test the result. You should be denied from accessing the [Protected.aspx] page, as well as
pages under the [MemberOnly] and [AdminOnly] folder.
• Add a new C# class file named [Security.cs] under the root folder:
NOTE: You can also add an using statement automatically by placing the mouse cursor on
the targeted class (e.g. Encoding) and press [Ctrl + .] to activate and select an option (e.g.
add using statement) from the smart tag menu.
• Within the class, create a new method named GetHash. The method should convert a
plaintext password to its base64 encoded hash by using the SHA-256 hash function:
Convert string
to byte array
Double-click on this
CustomValidator
• Double-click on the CustomValidator with error message [The [Username] has been used].
Program its ServerValidate event handler to detect duplicated username:
• Switch to Source View. Write the following HTML and JavaScript codes to redirect the user
to [Login.aspx] after 3 seconds:
• Test the result. Try to register a new Member account. Once register successfully, you
should be redirected to the [Successful.aspx] page. After 3 seconds, you will be redirected
again to the [Login.aspx] page.
CustomValidtor: cvNotMatched
• Double-click on btnLogin. Program the btnLogin_Clicked event handler to login the user if
the login credentials are matched. Otherwise, display the relevant error message:
Otherwise, display
the error message
• Program the Page_Load event handler to logout the user if he is an authenticated user.
Force the browser to reload the page after logout:
If it is an authenticated user
• Test the result. Try to login by using any of the Member or Admin accounts available in the
database. Passwords for all the default accounts are [password]. After login, you should be
able to access to the [Protected.aspx] page. Try to logout also.
• However, you still unable to access to the pages under the [MemberOnly] and [AdminOnly]
folders even if you login with the relevant Member or Admin account. This is because forms
authentication does not role-based authorization by default.
• Open the data file named [Code Snippets.txt] as given to you. Copy all the codes in the data
file and paste them within the Security class block. You should now have 2 additional public
static methods in the class: LoginUser and ProcessRoles.
• Open the [Login.aspx] page. Modify the btnLogin_Clicked event handler as shown below:
• In Solution Explorer, add the [Global.asax] file under the root folder.
NOTE: The event handler is not in the [Global.asax] file by default. You will have to add it
manually (you can copy-and-paste other event handler and modify accordingly).
• Test the result. Try to login by using any of the Member or Admin accounts available in the
database. Passwords for all the default accounts are [password]. After login, you should be
able to access to the pages under the [MemberOnly] or [AdminOnly] folder now (depending
if you are using Member or Admin account).
• Currently, the menu displays all menu items regardless if the user has access to them. We
want to show ONLY menu items that are accessible by the current user:
• In Solution Explorer, open the [Web.config] file under the root folder.
• Add a new <siteMap> element under the hierarchy [configuration > system.web]. Type the
XML codes as shown below:
• Within the same [Web.config] file, locate the <location> element we have created earlier.
Add 3 more <location> elements to refine our authorization rules:
• Open the [Logout.aspx] page. Modify the Page_Load event handler as shown below:
Remove or comment
out this original line
• Open the [Web.sitemap] XML file. Add the roles attribute to the Member and Admin
sitemap nodes:
• Test the result. The menu should now display (or hide) menu items depending on the user
login status and role:
Before login
• Drag-and-drop a LoginView control and a LoginStatus control from the Toolbox to the
footer section of the master page as shown below:
LoginImageUrl = ~/Images/Login.png
LogoutImageUrl = ~/Images/Logout.png
• Test the result. Try to login and examine the result at the footer section of the page:
Username of the
user is shown After login: Click
the image button
to logout