如何利用ASP给主页加密 |
作者:佚名 发布时间:2005-04-02 来源:不详
|
下面我就向大家介绍如何用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
% >
|
|
|
|
|