Microsoft KB Archive/321439

= How to use a database for user names and passwords in FrontPage 2002 =

Article ID: 321439

Article Last Modified on 2/1/2007

-

APPLIES TO


 * Microsoft FrontPage 2002 Standard Edition

-



This article was previously published under Q321439





For a Microsoft FrontPage 2000 version of this article, see 321503.



IN THIS TASK

 * SUMMARY
 * Step 1 - Get ready to use the ASP features in FrontPage 2002
 * Step 2 - Create a new Web in FrontPage 2002
 * Step 3 - Create a database by using Microsoft Access
 * Step 4 - Import the Microsoft Access database
 * Step 5 - Create the ASP pages
 * Step 5a - Create the home page
 * Step 5b - Create an unsecure page
 * Step 5c - Create a secure page
 * Step 5d - Create the logon page
 * Step 5e - Create the logon include file
 * Step 6 - Test the logon Web
 * Customize the logon sample
 * REFERENCES



SUMMARY
This step-by-step article explains how to create a simple file security solution by using Microsoft FrontPage 2002, Active Server Pages (ASP), and a Microsoft Access database.

Important notes

 * The sample code in this article is not designed as a replacement for the FrontPage built-in security functionality. The samples are designed to provide a simple security mechanism only for users who are browsing to your Web site. As such, FrontPage 2002 security does not integrate with the user names and passwords that are added to the Microsoft Access database.
 * The user names and passwords that are typed in are transmitted across the Internet in plain text. For greater security, Microsoft recommends that you use a Web server that can use Secure Sockets Layer (SSL) encryption. For more information, please ask your Web site administrator or Internet Service Provider (ISP).

back to the top

Step 1 - Get ready to use the ASP features in FrontPage 2002
Before you can use the ASP features in FrontPage 2002, you must install the components that are listed in the following Microsoft Knowledge Base article:

318287 What you need to use Active Server Pages (ASP) in FrontPage 2002

Step 2 - Create a new Web in FrontPage 2002
Note For this sample code to work correctly, you must make sure that the Web name is LOGON, and that it is a subweb off the root of your Web site.  On the File menu, click New, and then click Page or Web. In the task pane, click Web Site Templates. In the list of templates, click Empty Web. Specify the location of the Web on your server by using the following syntax

http:// /logon/

where  is the name of your ASP-enabled Web server. Click OK.</ol>

back to the top

Step 3 - Create a database by using Microsoft Access
<ol> Start Microsoft Access.</li> On the File menu, click New.</li> In the list of choices, click Blank Database.</li> Name the file logon.mdb and save it to your desktop.</li> In the Tables section, click New.</li> In the list, click Design View, and then click OK.</li> Create two fields: <ol style="list-style-type: lower-alpha;"> For the Field Name, type UID. For the Data Type, click Text, and then click Primary Key.</li> For the Field Name, type PWD. For the Data Type, click Text.</li></ol> </li> On the File menu, click Save. Name the table tblUsers, and then click OK.</li> On the View menu, click Datasheet View.</li> In the UID column, type testuser. In the PWD column, type password .</li> On the File menu, click Close to close the table.</li> Quit Microsoft Access.</li></ol>

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

back to the top

Step 4 - Import the Microsoft Access database

 * 1) On the File menu, click Import.
 * 2) Click Add File.
 * 3) In the Look in list, click your desktop.
 * 4) Click the logon.mdb file that you created in Step 3, and then click Open.
 * 5) Click Modify.
 * 6) Change the URL to _private/logon.mdb, and then click OK.
 * 7) Click OK to import the file.
 * 8) If you are prompted to add a database connection, click No.

back to the top

Step 5 - Create the ASP pages
You must create several files to work with this sample. First, you create a home page for your Web site, an unsecure page and a secure page for testing, and then the logon Web page and the logon include file.

Step 5a - Create the Home Page
This page serves as the default page for your site and includes links to the unsecure page and secure page that you will create later. <ol> On the FrontPage toolbar, click New Page.</li> Switch to HTML view, and then delete all the existing HTML code.</li> <li> Type or paste the following code into the 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 %> <ul> <li><a href=&quot;secure.asp&quot;>Secure Page</a></li> <li><a href=&quot;unsecure.asp&quot;>Unsecure Page</a></li> </ul> </li> <li>Save the page as default.asp in the root folder of your Web.</li> <li>Close the page by clicking Close on the File menu.</li></ol>

back to the top

Step 5b - Create an unsecure page
This page is a basic ASP page that anyone can browse. <ol> <li>On the FrontPage toolbar, click New Page.</li> <li>Switch to HTML view, and then delete all the existing HTML code.</li> <li> Type or paste the following code into the page: <% @language=&quot;vbscript&quot; %> Unsecure Page Unsecure 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> <li>Save the page as default.asp in the root folder of your Web.</li> <li>Close the page by clicking Close on the File menu.</li></ol>

back to the top

Step 5c - Create a secure page
The page in this step is the same as the unsecure page that you created in Step 5b, except that you add the following line of code near the top of the page: Adding this line of code to any ASP Web page makes the page a &quot;secure&quot; Web page. <ol> <li>On the FrontPage toolbar, click New Page.</li> <li>Switch to HTML view, and then delete all the existing HTML code.</li> <li> Type or paste the following code into the page: <% @language=&quot;vbscript&quot; %>

Secure Page Secure 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> <li>Save the page as secure.asp in the root folder of your logon Web.</li> <li>Close the page by clicking Close on the File menu.</li></ol>

back to the top

Step 5d - Create the logon page
The logon page is designed to resemble a standard Windows logon dialog box. Users who try to access the secure page are sent to this page to type their user name and password. <ol> <li>On the FrontPage toolbar, click New Page.</li> <li>Switch to HTML view, and then delete all the existing HTML code.</li> <li> Type or paste the following code into the 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.</li> <li>Close the page by clicking Close on the File menu.</li></ol>

back to the top

Step 5e - Create the logon include file
This include file provides the user name and password functionality and is used by both the secure page and the logon page. <ol> <li>On the FrontPage toolbar, click New Page.</li> <li>Switch to HTML view, and then delete all the existing HTML code.</li> <li> Type or paste the following code into the 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.</li> <li>Close the page by clicking Close on the File menu.</li></ol>

back to the top

Step 6 - Test the logon web

 * 1) In the FrontPage Folder List, click default.asp. On the FrontPage toolbar, click Preview in Browser.
 * 2) Your browser now loads the sample home page and shows that you are not logged on.
 * 3) Click the link for the unsecure page. The page loads and shows that you are not logged on. Click the link back to the default page.
 * 4) Click the link to the secure page. The logon page loads instead of the secure page.
 * 5) Type testuser for the user name, type password for the password, and then click LOGON.
 * 6) The secure page appears and shows that you are logged on as testuser. Click the link back to the default page.
 * 7) The sample home page loads and shows that you are logged on as testuser.
 * 8) Click the link for the unsecure page. The page loads and shows that you are logged on as testuser.

back to the top

Customize the logon sample
You can customize the logon sample in the following ways: <ul> <li>Add user names and passwords: You can open the database by double-clicking it in FrontPage and then add users to the tblUsers table.</li> <li> Secure other Web pages: To secure another Web page in your Web, you must save the file with an ASP file name extension, for example, mypage.asp, and then add the following two lines to the very top of the file: <% @language=&quot;vbscript&quot; %>

The first line specifies that you are using Microsoft Visual Basic Scripting Edition (VBScript) for your scripting language, and the second line includes the user name and password functionality from the logon include file that you created earlier. </li></ul>

back to the top

<div class="references_section">