Microsoft KB Archive/928706

= How to create a Web page that is protected by password by using Expression Web, Active Server Pages, and an Access database =

Article ID: 928706

Article Last Modified on 4/17/2007

-

APPLIES TO


 * Microsoft Expression Web

-



SUMMARY
This step-by-step article describes how to create a simple Web page solution that provides password protection by using Microsoft Expression Web, Active Server Pages (ASP), and a Microsoft Access database.
 * The sample code in this article is not designed to replace the Expression Web built-in security functionality. The samples are designed to provide a simple security mechanism only for users who are browsing to your Web site. Therefore, Expression Web security does not integrate with the user names and the passwords that are added to the Access database.
 * The user names and passwords that are typed in are transmitted across the Internet in plain text. To help increase security, we recommend that you use a Web server that can use Secure Sockets Layer (SSL) encryption. For more information, contact your Web site administrator or your Internet service provider (ISP).



Introduction
This article describes how to create a Web page that is protected by password by using Expression Web, ASP, and an Access database.



Use the ASP features in Expression Web
Before you use the ASP features in Expression Web, you must have access to a Web server or a disk-based Web that supports ASP.

Create a new Web site in Expression Web
Note The example information assumes that you name a Web site logon and that the logon site is created as a sub Web under the root of your Web site. If you use a name other than logon, or create a Web site in an alternative location, you must modify the steps throughout this article accordingly.

To create a new Web site in Expression Web, follow these steps:
 * 1) Start Expression Web.
 * 2) On the File menu, click New, and then click Web site.
 * 3) In the New dialog box, click Empty Web Site in the Web Site tab.
 * 4) In the Specify the location of the new Web site box, type the location where you want to store the new Web site in the following format:

http://servername/logon
 * 1) Click OK.

The new empty Web site that is named logon is opened in Expression Web.

Create a database
You can create database to store user names and passwords by using a database program such as Microsoft Office Access 2003.

Note If you use a database program other than Access 2003 to create the database, modify these steps accordingly.

To create a database, follow these steps:
 * 1) Start Access 2003.
 * 2) On the File menu, click New.
 * 3) In the task pane, click Blank database.
 * 4) Type logon.mdb for the file name, and then click Create.
 * 5) In the Objects pane, click Tables, and then click New.
 * 6) Click Design View, and then click OK.
 * 7) In the first row of the Field Name column, type UID.
 * 8) In the corresponding Data Type column, click Text, and then click the Primary Key button on the toolbar.

Note The Primary Key button appears as a key symbol.
 * 1) In the second row of the Field Name column, type PWD, and then click Text in the second row of the Data Type column.
 * 2) On the File menu, click Save.
 * 3) In the Table Name box, type tblUsers, and then click OK.
 * 4) On the View menu, click Datasheet View.
 * 5) In the UID column, type testuser.

In the PWD column, type password.
 * 1) On the File menu, click Close, and then exit Access 2003.

Note For security reasons, passwords are restricted to a mixture of uppercase letters, lowercase letters, and numbers.

Import the database
Import the database that you created to store user names and passwords into Expression Web. To do this, follow these steps:
 * 1) In Expression Web, open your logon Web site, click Import on the File menu, and then click File.
 * 2) Click Add File, locate and then click the logon.mdb file that you created, and then click Open.
 * 3) Click Modify, type _private/logon.mdb in the File location within your web text box, and then click OK.

Note There is an underscore character (_) in front of &quot;private&quot; in the path of the file.
 * 1) Click OK to import the database file.
 * 2) If you are prompted to create a database connection for this imported file, click No.

Create the ASP pages
You must create several files to work with this sample. First, create a home page for your Web site, a &quot;nonsecure&quot; page and a page that is protected for testing, the logon Web page, and the logon include file.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure. However, they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Create the home page
This page serves as the default page for your site. It includes links to the nonsecure page and to the protected Web page that you create. To create a home page, follow these steps:  In Expression Web, click New on the File menu, and then click Page. In the New dialog box, click HTML in the Page tab, and then click OK. In the footer area of the document window, click Code to show code view. Select and then remove all the HTML code in the Web page.  Type or paste the following HTML code in the Web page. <% @language=&quot;vbscript&quot; %> Home Page Home Page You are logged on as: <% If Len(Session(&quot;UID&quot;)) = 0 Then Response.Write &quot;You are not logged on.&quot; Else Response.Write &quot;&quot; & Session(&quot;UID&quot;) & &quot;&quot; End If %>  Password-Protected Page</a></li> <a href=&quot;nonsecure.asp&quot;>Nonsecure Page</a></li>

</ul> </li> Save the page as default.asp in the root folder of your logon Web site.</li> On the File menu, click Close to close the default.asp Web page.</li></ol>

Create the nonsecure page
Create a typical ASP page that everyone can view. To create a nonsecure page, follow these steps: <ol> In Expression Web, click New on the File menu, and then click Page.</li> In the New dialog box, click HTML in the Page tab, and then click OK.</li> In the footer area of the document window, click Code to show code view.</li> Select and then remove all the HTML code in the Web page.</li>  Type or paste the following HTML code in the Web page. <% @language=&quot;vbscript&quot; %> Nonsecure Page Nonsecure Page You are logged on as: <% If Len(Session(&quot;UID&quot;)) = 0 Then Response.Write &quot;You are not logged on.&quot; Else Response.Write &quot;&quot; & Session(&quot;UID&quot;) & &quot;&quot; End If %> <a href=&quot;default.asp&quot;>Back to default</a> </li> Save the page as nonsecure.asp in the root folder of your logon Web site.</li> On the File menu, click Close to close the nonsecure.asp Web page.</li></ol>

Create the protected page
The page in this step is the same as the nonsecure Web page that you created previously, except that you must add the following code near the top of code view: When you add this line of code to an ASP page, that page becomes protected by password by the logon.inc file that you create. To create a Web page that is protected by password, follow these steps: <ol> In Expression Web, click New on the File menu, and then click Page.</li> In the New dialog box, click HTML in the Page tab, and then click OK.</li> In the footer area of the document window, click Code to show code view.</li> Select and then remove all the HTML code in the Web page.</li>  Type or paste the following HTML code in the Web page. <% @language=&quot;vbscript&quot; %>

Password-Protected Page Password-Protected Page You are logged on as: <% If Len(Session(&quot;UID&quot;)) = 0 Then Response.Write &quot;You are not logged on.&quot; Else Response.Write &quot;&quot; & Session(&quot;UID&quot;) & &quot;&quot; End If %> <a href=&quot;default.asp&quot;>Back to default</a> </li> Save the page as passwordprotect.asp in the root folder of your logon Web site.</li> <li>On the File menu, click Close to close the passwordprotect.asp Web page.</li></ol>

Create the logon page
Create a logon page resembles a typical Windows logon dialog box. Users who try to access the protected Web page are sent to this page to type their user names and passwords. To create a logon page, follow these steps: <ol> <li>In Expression Web, click New on the File menu, and then click Page.</li> <li>In the New dialog box, click HTML in the Page tab, and then click OK.</li> <li>In the footer area of the document window, click Code to show code view.</li> <li>Select and then remove all the HTML code in the Web page.</li> <li> Type or paste the following HTML code in the Web page. <% @language=&quot;vbscript&quot; %>

<% ' Was this page posted to? If UCase(Request.ServerVariables(&quot;HTTP_METHOD&quot;)) = &quot;POST&quot; Then ' If so, check the username/password that was entered. If ComparePassword(Request(&quot;UID&quot;),Request(&quot;PWD&quot;)) Then ' If comparison was good, store the user name... Session(&quot;UID&quot;) = Request(&quot;UID&quot;) ' ...and redirect back to the original page. Response.Redirect Session(&quot;REFERRER&quot;) End If End If %> Logon Page body { font-family: arial, helvetica } table { background-color: #cccccc; font-size: 9pt; padding: 3px } td   { color: #000000; background-color: #cccccc; border-width: 0px } th   { color: #ffffff; background-color: #0000cc; border-width: 0px } <body bgcolor=&quot;#000000&quot; text=&quot;#ffffff&quot;> <h3 align=&quot;center&quot;>&#xa0; <div align=&quot;center&quot;> <form action=&quot;<%=LOGON_PAGE%>&quot; method=&quot;POST&quot;> </li> <li>Save the page as logon.asp in the root folder of your logon Web site.</li> <li>On the File menu, click Close to close the logon.asp Web page.</li></ol>

Create the logon include file
The include file provides the user name and password functionality. It can be used by both the protected Web page and the logon Web page. To create the logon include file, follow these steps: <ol> <li>In Expression Web Designer, click New on the File menu, and then click Page.</li> <li>In the New dialog box, click HTML in the Page tab, and then click OK.</li> <li>In the footer area of the document window, click Code to show code view.</li> <li>Select and then remove all the HTML code in the Web page.</li> <li> Type or paste the following HTML code in the Web page. <% ' Do not cache this page. Response.CacheControl = &quot;no-cache&quot;

' Define the name of the users table. Const USERS_TABLE = &quot;tblUsers&quot; ' Define the path to the logon page. Const LOGON_PAGE  = &quot;/logon/logon.asp&quot; ' Define the path to the logon database. Const MDB_URL     = &quot;/logon/_private/logon.mdb&quot;

' Check to see whether you have a current user name. If Len(Session(&quot;UID&quot;)) = 0 Then ' Are you currently on the logon page? If LCase(LOGON_PAGE) <> LCase(Request.ServerVariables(&quot;URL&quot;)) Then ' If not, set a session variable for the page that made the request... Session(&quot;REFERRER&quot;) = Request.ServerVariables(&quot;URL&quot;) ' ...and redirect to the logon page. Response.Redirect LOGON_PAGE End If End If

' This function checks for a username/password combination. Function ComparePassword(UID,PWD) ' Define your variables. Dim strSQL, objCN, objRS ' Set up your SQL string. strSQL = &quot;SELECT * FROM &quot; & USERS_TABLE & _ &quot; WHERE (UID='&quot; & ParseText(UID) & _     &quot;' AND PWD='&quot; & ParseText(PWD) & &quot;');&quot; ' Create a database connection object. Set objCN = Server.CreateObject(&quot;ADODB.Connection&quot;) ' Open the database connection object. objCN.Open &quot;driver={Microsoft Access Driver (*.mdb)}; dbq=&quot; & _ Server.MapPath(MDB_URL) & &quot;; uid=admin; pwd=&quot; ' Run the database query. Set objRS = objCN.Execute(strSQL) ' Set the status to true/false for the database lookup. ComparePassword = Not(objRS.EOF) ' Close your database objects. Set objRS = Nothing Set objCN = Nothing End Function

' This function restricts text to alpha-numeric data only. Function ParseText(TXT) Dim intPos, strText, intText For intPos = 1 TO Len(TXT) intText = Asc(Mid(TXT,intPos,1)) If (intText > 47 And intText < 58) Or _ (intText > 64 And intText < 91) Or _ (intText > 96 And intText < 123) Then strText = strText & Mid(TXT,intPos,1) End if   Next ParseText = strText End Function %> </li> <li>Save the page as logon.inc in the _private folder of your logon Web site.</li> <li>On the File menu, click Close to close the logon.inc Web page.</li></ol>

Test the logon Web site
To do this, follow these steps:
 * 1) In Expression Web, right-click Default.asp in the Folder List pane, and then click Preview in Browser.

The Web browser loads the sample home page and shows that you are not logged on.
 * 1) Click the Nonsecure page link.

The page loads and shows that you are not logged on. Click the Back to default link to return to the default page.
 * 1) Click the Password-Protected page link.

The logon.asp page loads instead of the protected page.
 * 1) In the User Name box, type testuser, type password in the Password box, and then click LOGON.

The protected page appears and shows that you are logged on as testuser. Click the Back to default link to return to the default page. The home page loads and shows that you are logged on as testuser.
 * 1) Click the Nonsecure page link.

The page loads and shows that you are logged on as testuser.

Customize the logon Web site
You can customize the logon example Web site in the following ways: <ul> <li>Add user names and passwords:

You can open the database in Expression Web, and then add users to the tblUsers table.</li> <li> Create other Web pages that are protected by password:

To create another Web page that is protected by password in your Web site, you must save the file by using an .asp file name extension, such as Mypage.asp, and then add the following two lines to the very top of code view: <% @language=&quot;vbscript&quot; %> The first line specifies that you are using Microsoft Visual Basic Scripting Edition (VBScript) for your scripting language. The second line includes the user name and the password functionality from the logon include file that you created earlier. </li></ul>

<div class="references_section">