下面我就向大家介紹如何用ASP來建立一個簡單的,但是卻相當有效登入方案,從而保證WEB應用程式的安全性。只要你按照以下的步驟做,你將會擁有一個安全的使用者登入系統。
步驟1:建立一個使用者表
首先我們要建立一個記錄受權使用者資訊的表格。 在這個例子中,我用ACCESS97建立了一個名叫userinfo.mdb的資料庫,它包含了受權使用者的資訊,使用者有兩個欄位——使用者名稱和使用者密碼,其中使用者名稱為主關鍵字。(之所以選用ACCESS,是因為它廣為人所知,使用方便,並且適用於大多數的中小型方案。)
步驟2:設定預設的驗證狀態
我們要在global.asa檔案中完成這些設定,為預設的“not authenticated”狀態設定一個對話變數。這樣做後,當使用者想訪問那些受到保護的頁面時,只有你檢查過他們的身份證明後,他們才可以訪問。預設狀態的設定確保了每個人在進入網頁之前都經過了身分識別驗證。
在global.asa檔案中,Session_OnStart事件裡,填寫以下代碼
在global.asa檔案中,Session_OnStart事件裡,填寫以下代碼
< SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Session_OnStart
……
……
’ This is the default authentication status
Session(“Authenticated”) = 0
END SUB
< /SCRIPT>
切記驗證身份狀態的設定是一件非常重要的事物,請不要忘記。 我們的目的是:
*驗證使用者是否經過授權並根據結果設定相應的驗證狀態
*如果使用者是經過授權的,驗證狀態置1
*如果使用者是沒有授權的,驗證狀態置0
下面顯示的是verify.asp頁的代碼,你可以根據實際情況作一些相應的修改。
< %
’ Create a command object. This object serves to run our queries
Set Cm = Server.CreateObject(“ADODB.Command”)
’ Specify the system DSN path
Cm.ActiveConnection =“LoginDSN”
’ Now it’s time for the query. We need to check the user information
’ against the table tUsers
Cm.CommandText=“SELECT * FROM tUsers WHERE ”& _
“UserName=’”& Request.Form(“UserName”) &“’ AND ”& _
“UserPassword=’” & Request.Form(“UserPassword”) & “’”
’ Set the query type. 1 means it is a SQL statement
Cm.CommandType = 1
’ Retrieve the results in a recordset object
Set Rs = Cm.Execute
’ We now check if the user is valid. If user is valid, the recordset MUST
’ haverecord. Otherwise it is empty. If user exists, we set authentication
’ status to 1 and send the user to appropriate page, say welcome.asp.
’ Else send the user back to login.asp
If Rs.EOF Then
Session(“Authenticated”) = 0
Response.Redirect (“login.asp”)
Else
Session(“Authenticated”) = 1
Response.Redirect (“welcome.asp”)
End If
% >