日期:2011-07-26  浏览次数:20709 次

利用ASP给主页加密

我们的目的是:

  *验证用户是否经过授权并根据结果设置相应的验证状态

  *如果用户是经过授权的,验证状态置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

% >

步骤6:检查验证状态

  这是我们系统中的一个重要的部分。我们要在每个受到保护的页面上,检查用户的验证状态。这个做起来很简单。只要检查用户的验证状态是否为1即可,如果不是1则把用户重新送到login.asp页。示例代码如下

< %

If Session(“Authenticated”) = 0 Then

Response.Redirect (“Login.asp”)

End If

% >

  你还可以用另一种方法。把上面的代码拷贝到一个叫check.inc的文件中,再把下面这行代码包含到需要保护的页面的头部。

  如果你有很多页面需要保护,你只需把这行代码写到每一个需要保护的网页的头部即可。

  上述6个步骤将帮助你创建一个简单的用户登录系统。但请记住这个系统只是保护一个虚拟的目录,而不是整个的网站。你需要为每个你想要保护的虚拟路径创建一个。