本文將逐步說明如何使用 Microsoft FrontPage 2000]、 [動態伺服器網頁 (ASP)] 及 [Microsoft Access 資料庫中建立簡單的檔案安全性解決方案。
重要注意事項
- 本文中的範例程式碼不被設計為用來取代 FrontPage 內建的安全性功能。範例是設計用來提供簡單的安全性機制只針對的瀏覽至您的網站的使用者。像這類 FrontPage 2000 安全性並不整合與使用者名稱及密碼新增至 Microsoft Access 資料庫。
- 使用者名稱和在輸入的密碼會以純文字在網際網路上傳輸。更高安全性的 Microsoft 建議您使用 Web 伺服器,可以使用安全通訊端層 (SSL) 加密。如需詳細資訊,請詢問網站管理員或網際網路服務提供者 (ISP)。
步驟 1-取得就緒要在 FrontPage 2000 中使用 ASP 功能
您可以在 FrontPage 2000 中使用 ASP 功能之前,您必須安裝下列 「 Microsoft 知識庫 」 文件中所列的元件:
312638?
(http://support.microsoft.com/kb/312638/
)
項目所需在 FrontPage 2000 中使用動態伺服器網頁 (ASP) 的描述
步驟 2-在 FrontPage 2000 建立新的網頁
附註如需此程式範例才能正常運作中,您必須確定 Web name 是登入,它是關閉您的網站的根目錄的子 Web。
- 在 [檔案] 功能表上按一下 [新增],然後按一下 [Web]。
- 在範本清單中,按一下 [空白網頁]。
- 使用下列語法來指定您伺服器上的 Web 位置
your server /logon/ http://
其中 your server 是您的 ASP 啟用 Web 伺服器的名稱。
- 按一下 [確定]。
步驟 3-使用 Microsoft Access 中建立資料庫
- 啟動 Microsoft Access。
- 按一下 [檔案] 功能表 新增]。
- 在選擇清單中按一下 [空白資料庫]。
- 命名檔案 logon.mdb,然後將它儲存到您的桌面。
- 在 [資料表] 區段中,按一下 [新增]。
- 按一下 [設計檢視],然後再按一下 [確定]。
- 建立兩個欄位:
- 欄位名稱 輸入 UID。如 資料類型 按一下 [文字,] 然後按一下 [主索引鍵]。
- 欄位名稱 輸入 PWD。資料類型 按一下 [文字]。
- 在 [檔案] 功能表上按一下 [儲存]。命名表格 tblUsers,然後按一下 [確定]。
- 按一下 [檢視] 功能表 資料工作表檢視。
- 在 [UID] 欄中,鍵入 testuser。在 [PWD] 欄中,鍵入 密碼。
- 在 [檔案] 功能表上, 按一下 [關閉]。
- 請結束部份的 Microsoft Access。
附註基於安全性考量密碼是限於混合大寫字母、 小寫字母及數字。
步驟 4-匯入 Microsoft Access 資料庫
- 按一下 [檔案] 功能表 匯入。
- 按一下 [新增檔案]。
- 在 [查詢] 清單中,按一下您的桌面。
- 按一下您在 Step
3,logon.mdb 檔案,然後按一下 [開啟舊檔]。
- 按一下 [修改]。
- 以 _private/logon.mdb,變更 URL,然後按一下 [確定]。
- 按一下 [確定],匯入檔案。
- 如果系統提示您新增資料庫連線,請按一下 [否]。
步驟 5-建立 ASP 網頁
您必須建立數個檔案來使用這個範例。建立您的網站]、 [
unsecure page] 及 [
secure page 進行測試,一個
home page 的第一次,然後
logon Web page 和
logon include file。
步驟 5a-建立首頁
此頁面來當做您的網站預設的網頁,包含
unsecure page 和稍後建立
secure page 的連結。
- 在 FrontPage] 工具列上按一下 [新增頁面]。
- 切換至 HTML 檢視,然後再刪除所有現有的 HTML 程式碼。
- 輸入或貼上下列程式碼插入網頁:
<% @language="vbscript" %>
<html>
<head><title>Home Page</title></head>
<body>
<h3>Home Page</h3>
<p>You are logged on as:
<%
If Len(Session("UID")) = 0 Then
Response.Write "<b>You are not logged on.</b>"
Else
Response.Write "<b>" & Session("UID") & "</b>"
End If
%>
</p>
<ul>
<li><a href="secure.asp">Secure Page</a></li>
<li><a href="unsecure.asp">Unsecure Page</a></li>
</ul>
</body>
</html>
- 將頁面儲存為 default.asp 您登入 Web 的根資料夾中。
- 按一下 [檔案] 功能表上的 [關閉] 關閉 [頁]。
步驟 5b]-建立不安全的頁面
此頁面是基本的 ASP 網頁,任何人都可以瀏覽。
- 在 FrontPage] 工具列上按一下 [新增頁面]。
- 切換至 HTML 檢視,然後再刪除所有現有的 HTML 程式碼。
- 輸入或貼上下列程式碼插入網頁:
<% @language="vbscript" %>
<html>
<head><title>Unsecure Page</title></head>
<body>
<h3>Unsecure Page</h3>
<p>You are logged on as:
<%
If Len(Session("UID")) = 0 Then
Response.Write "<b>You are not logged on.</b>"
Else
Response.Write "<b>" & Session("UID") & "</b>"
End If
%>
</p>
<p><a href="default.asp">Back to default</a></p>
</body>
</html>
- 將頁面儲存為您的登入 Web 的根資料夾中的 unsecure.asp"。
- 按一下 [檔案] 功能表上的 [關閉] 關閉 [頁]。
步驟 5 c-建立安全的網頁
這個步驟中是為您在
Step 5b 中, 建立的非安全網頁相同,只不過新增下行程式碼在頁面頂端附近的:
<!--#include virtual="/logon/_private/logon.inc"-->
任何 ASP 網頁中加入這一行程式碼讓分頁 「 安全 」 的 Web 網頁。
- 在 FrontPage] 工具列上按一下 [新增頁面]。
- 切換至 HTML 檢視,然後再刪除所有現有的 HTML 程式碼。
- 輸入或貼上下列程式碼插入網頁:
<% @language="vbscript" %>
<!--#include virtual="/logon/_private/logon.inc"-->
<html>
<head><title>Secure Page</title></head>
<body>
<h3>Secure Page</h3>
<p>You are logged on as:
<%
If Len(Session("UID")) = 0 Then
Response.Write "<b>You are not logged on.</b>"
Else
Response.Write "<b>" & Session("UID") & "</b>"
End If
%>
</p>
<p><a href="default.asp">Back to default</a></p>
</body>
</html>
- 將頁面儲存為 secure.asp 您登入 Web 的根資料夾中。
- 按一下 [檔案] 功能表上的 [關閉] 關閉 [頁]。
步驟 5 d-建立登入頁面
登入頁面的設計被為了類似標準的 Windows 登入對話方塊。嘗試存取
secure page 的使用者會傳送到此頁面以輸入其使用者名稱和密碼。
- 在 FrontPage] 工具列上按一下 [新增頁面]。
- 切換至 HTML 檢視,然後再刪除所有現有的 HTML 程式碼。
- 輸入或貼上下列程式碼插入網頁:
<% @language="vbscript" %>
<!--#include virtual="/logon/_private/logon.inc"-->
<%
' Was this page posted to?
If UCase(Request.ServerVariables("HTTP_METHOD")) = "POST" Then
' If so, verify the username/password that was entered.
If ComparePassword(Request("UID"),Request("PWD")) Then
' If comparison was good, store the user name...
Session("UID") = Request("UID")
' ..and redirect back to the original page.
Response.Redirect Session("REFERRER")
End If
End If
%>
<html>
<head><title>Logon Page</title>
<style>
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 }
</style>
</head>
<body bgcolor="#000000" text="#ffffff">
<h3 align="center"> </h3>
<div align="center"><center>
<form action="<%=LOGON_PAGE%>" method="POST">
<table border="2" cellpadding="2" cellspacing="2">
<tr>
<th colspan="4" align="left">Enter User Name and Password</th>
</tr>
<tr>
<td> </td>
<td colspan="2" align="left">Please type your user name and password.</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="left">Site</td>
<td align="left"><%=Request.ServerVariables("SERVER_NAME")%>  </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="left">User Name</td>
<td align="left"><input name="UID" type="text" size="20"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="left">Password</td>
<td align="left"><input name="PWD" type="password" size="20"></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2" align="center"><input type="submit" value="LOGON"></td>
<td> </td>
</tr>
</table>
</form>
</center></div>
</body>
</html>
- 將頁面儲存為 logon.asp 您登入 Web 的根資料夾中。
- 按一下 [檔案] 功能表上的 [關閉] 關閉 [頁]。
步驟 5e-建立登入要求包含檔案
此包含檔案提供使用者名稱和密碼功能,而會使用
secure
page 和
logon
page。
- 在 FrontPage] 工具列上按一下 [新增頁面]。
- 切換至 HTML 檢視,然後再刪除所有現有的 HTML 程式碼。
- 輸入或貼上下列程式碼插入網頁:
<%
' Do not cache this page.
Response.CacheControl = "no-cache"
' Define the name of the users table.
Const USERS_TABLE = "tblUsers"
' Define the path to the logon page.
Const LOGON_PAGE = "/logon/logon.asp"
' Define the path to the logon database.
Const MDB_URL = "/logon/_private/logon.mdb"
' Check to see whether you have a current user name.
If Len(Session("UID")) = 0 Then
' Are you currently on the logon page?
If LCase(LOGON_PAGE) <> LCase(Request.ServerVariables("URL")) Then
' If not, set a session variable for the page that made the request...
Session("REFERRER") = Request.ServerVariables("URL")
' ...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 = "SELECT * FROM " & USERS_TABLE & _
" WHERE (UID='" & ParseText(UID) & _
"' AND PWD='" & ParseText(PWD) & "');"
' Create a database connection object.
Set objCN = Server.CreateObject("ADODB.Connection")
' Open the database connection object.
objCN.Open "driver={Microsoft Access Driver (*.mdb)}; dbq=" & _
Server.MapPath(MDB_URL) & "; uid=admin; pwd="
' 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
%> - 將頁面儲存為 logon.inc 您登入網頁的 [_private] 資料夾中。
- 按一下 [檔案] 功能表上的 [關閉] 關閉 [頁]。
步驟 6-測試登入網頁
- 在 FrontPage 資料夾清單] 中,按一下 default.asp。在 [FrontPage] 工具列上按一下 [在瀏覽器中的預覽]。
- 現在,您的瀏覽器載入範例首頁,並顯示您並未登入。
- 按一下不安全的頁面連結。網頁載入並顯示您並未登入。按一下 [回到預設網頁連結。
- 按一下連結以安全的網頁。載入登入頁面而非安全網頁。
- 輸入 testuser 的使用者名稱、 輸入該密碼的 密碼,然後按一下 [登入。
- 安全的網頁會出現,並顯示您登入為 testuser。按一下 [回到預設網頁連結。
- 範例首頁載入,並顯示您正在登入為 testuser。
- 按一下不安全的頁面連結。網頁載入並顯示您所登入為 testuser。
自訂登入範例
您可以下列方式自訂登入範例:
- 新增使用者名稱和密碼: 您可以開啟資料庫,方法是按兩下透過 FrontPage 然後新增使用者至 tblUsers 資料表。
- 保護其他 Web 網頁安全: 到安全的另一個網頁在您的網頁,您必須將檔案儲存的 ASP 副檔名的檔案,例如 mypage.asp,然後將下列兩行新增到檔案頂端:
<% @language="vbscript" %>
<!--#include virtual="/logon/_private/logon.inc"-->
的第一行您指令碼語言指定 [您正使用 Microsoft Visual Basic 指令碼版 (VBScript) 以及第二行包括使用者名稱和密碼功能,從 logon include file 先前建立之。
如更多有關資料庫和安全性與整合動態伺服器網頁 (ASP) 的資訊,按一下下面的文件編號,檢視 「 Microsoft 知識庫 」 中 「 文件:
299987?
(http://support.microsoft.com/kb/299987/
)
如何使用來實作 ASP 安全性的資料庫和 ASP 工作階段
300382?
(http://support.microsoft.com/kb/300382/
)
如何從 ASP 網頁建立資料庫連接