ASP 代码给 ASP 页加密码保护
生活随笔
收集整理的這篇文章主要介紹了
ASP 代码给 ASP 页加密码保护
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本任務的內容
概要
創建應用程序
測試應用程序
其他注意事項
疑難解答
參考
概要
本文演示如何編寫簡單的 Active Server Pages (ASP) 代碼來通過登錄頁對訪問進行限制。 本文介紹的都是一些簡化的方法。 如想獲得更多的功能或更可靠的安全性,請參見本文末尾的“參考”一節。
在本示例中,您將創建下面兩個 ASP 頁:
MyPage.asp: 此頁是受保護的,如果不提供正確的用戶名和密碼,則不能訪問此頁。
Logon.asp: 此頁提供了一個窗體,用戶可將其憑據鍵入此窗體中。 該窗體接著就會驗證用戶的用戶名和密碼。 如果用戶名和密碼正確,它就向客戶機寫入一個 Cookie,此 Cookie 就成了訪問其他 ASP 頁的“鑰匙”。
返回頁首
創建應用程序
使用“記事本”創建這些 ASP 頁。 要啟動“記事本”,請從 Windows 開始菜單上,指向程序,指向附件,然后單擊記事本。 將每個文檔保存到本地 Web 服務器的根 Web 目錄(通常是 C:/InetPub/Wwwroot/)下。 如要更改文檔的位置,則還必須對這些文件中的腳本進行相應的修改。
Logon.asp
在“記事本”中,單擊文件菜單中的新建。
選中以下代碼,右鍵單擊選中的內容,然后單擊快捷菜單中的復制。在“記事本”中,單擊編輯菜單上的粘貼以便將以下代碼粘貼到“記事本”中:
<html>
<head>
<title>Logon Form</title>
<%
Username="Administrator"
Password="Admin"
Validated = "OK"
if Strcomp(Request.Form("User"),Username,1)=0 AND Request.Form("password") = Password then
'Set the validation cookie and redirect the user to the original page.
???? Response.Cookies("ValidUser") = Validated
???? 'Check where the users are coming from within the application.
???? If (Request.QueryString("from")<>"") then
Response.Redirect Request.QueryString("from")
else
'If the first page that the user accessed is the Logon page,
???????? 'direct them to the default page.
?????????? Response.Redirect "MyPage.asp"
???? End if????
Else
' Only present the failure message if the user typed in something.
???? If Request.Form("User") <> "" then
???????? Response.Write "<h3>Authorization Failed.</h3>" & "<br>" & _
???????? "Please try again.<br> <br>"
???? End if
End if
%>
</head>
<body bgcolor="#FFFFFF">
<FORM ACTION=<%Response.Write "Logon.asp?"&Request.QueryString%> method="post">
<h3>Logon Page for MyPage.asp</h3>
<p>
Username:
<INPUT TYPE="text" NAME="User" VALUE='' size="20"></INPUT>
Password:
<INPUT TYPE="password" NAME="password" VALUE='' size="20"></INPUT>
<INPUT TYPE="submit" VALUE="Logon"></INPUT>
</FORM>
</body>
</html>
將此頁在 C:/InetPub/Wwwroot/ 文件夾中保存為 Logon.asp。
MyPage.asp
MyPage.asp 是您想要保護的頁。 您可以使用具有 .asp 文件擴展名的任何頁。
在“記事本”中,單擊文件菜單上的新建。
選中以下代碼,右鍵單擊選中的內容,然后單擊快捷菜單中的復制。在“記事本”中,單擊編輯菜單上的粘貼以便將以下代碼粘貼到“記事本”中:
<%
Validated = "OK"
if Request.Cookies("ValidUser") <> Validated then
'Construct the URL for the current page.
???? dim s
???? s = "http://";
???? s = s & Request.ServerVariables("HTTP_HOST")
???? s = s & Request.ServerVariables("URL")
???? if Request.QueryString.Count > 0 THEN
s = s & "?" & Request.QueryString
end if
???? 'Redirect unauthorized users to the logon page.
???? Response.Redirect "Logon.asp?from=" &Server.URLEncode(s)
End if
%>
<html>
<head>
<title>My Protected Page</title>
</head>
<body>
<p align="center">This is my secret information<br>
You cannot see it unless you<br>
are properly logged on!</p>
</body>
</html>
將此頁在 C:/InetPut/Wwwroot/ 文件夾中保存為 MyPage.asp。
返回頁首
測試應用程序
打開 Web 瀏覽器。 如果您使用的是 Microsoft Internet Explorer,請從 Windows 開始菜單上,指向程序,然后單擊 Internet Explorer。
在“地址”欄中鍵入下面的地址,然后按 ENTER 鍵:
http://localhost/MyPage.asp ;
您會注意到,您被重定向到 Logon.asp。
鍵入 Logon.asp 文件中 ASP 代碼中包含的用戶名和密碼信息(用戶名:Administrator,密碼:Admin),然后單擊登錄。 這樣您就應當可以看到 MyPage.asp 頁了。
請再鍵入一個錯誤的用戶名或密碼,以確認您無法登錄,因而也就不能瀏覽到 MyPage.asp。
返回頁首
其他注意事項
如想保護其他 ASP 頁,請將以下代碼添加到 ASP 頁的頂部,位于其他所有代碼之前:
<%
Validated = "OK"
if Request.Cookies("ValidUser") <> Validated then
'Construct the URL for the current page.
???? dim s
???? s = "http://";
???? s = s & Request.ServerVariables("HTTP_HOST")
???? s = s & Request.ServerVariables("URL")
???? if Request.QueryString.Count > 0 THEN
s = s & "?" & Request.QueryString
end if
???? 'Redirect unauthorized users to the logon page.
???? Response.Redirect "Logon.asp?from=" &Server.URLEncode(s)
End if
%>
若要登錄并重定向到所請求的受保護頁,必須讓超鏈接指向此頁面而非 Logon.asp 頁。 在本例中,要確保超鏈接指向 MyPage.asp。如果您未登錄,則此頁中包括的代碼將使您自動重定向到 Logon.asp。
如果確實想讓您站點的訪問者每次訪問時都登錄,則可以將 ValidUser 這一 Cookie 保存到他們的計算機上,以便他們下次訪問時能使用此信息。 一旦會話超時或您關閉瀏覽器窗口,前面的代碼就會導致 Cookie 過期。 如想設置 Cookie 的過期期限,請在 Logon.asp 中將下面的代碼
???????? Response.Cookies("ValidUser") = Validated
改為:
???????? Response.Cookies("ValidUser") = Validated
???????? Response.Cookies ("ValidUser").Expires = DATE + 1
若要指定過期期限,可將“1”改為所需的天數。 例如,下面的代碼會使 Cookie 在您的計算機上保留一年后過期:
???????? Response.Cookies ("ValidUser").Expires = DATE + 365
如果您設置了過期日期,則 Cookie 將保存到最終用戶的計算機上,這樣此用戶以后就可以繞過登錄頁。 不過,如果該用戶是從另一臺計算機上瀏覽到該站點的,則此 Cookie 就會保存到那一臺計算機上,這樣別人就有可能讀到并復制此信息。
返回頁首
缺陷
ASP 登錄頁可以用于很多應用程序,但它不能提供最高的安全性。 一般來說,NTFS 是最安全的。 NTFS 要求用戶鍵入 Microsoft Windows 可識別的用戶名和密碼。 NTFS 安全機制可用來為硬盤上的文件和文件夾設置訪問權限。
另外,ASP 安全性以 Microsoft Internet Information Server (IIS) 安全性為基礎。 如果未能妥善設置 IIS,而您添加了 ASP 安全功能,那么您不能防止高級用戶訪問您的站點。
有關 IIS 和 ASP 安全性的更多信息,請參見“參考”一節。
前面那段代碼只允許使用一組用戶憑據。 下面的 Microsoft 知識庫文章演示了如何使用一種在數據庫中保存了許多用戶名的 ASP 登錄頁:
Q299987 HOW TO: Use Database and ASP Sessions to Implement ASP Security(使用數據庫和 ASP 會話實現 ASP 安全)
如果您為 Cookie 設置了過期日期,則此 Cookie 就保存在瀏覽您的頁面時使用的那臺計算機上。 如果有人從咖啡館里的計算機等公用計算機上瀏覽您的頁面,那么 Cookie 就保存在此計算機上,于是其他人就有可能讀到并復制此信息。 如果不設置過期日期,則 Cookie 就不會保存到計算機的硬盤上(它只存儲在內存中),而且一旦瀏覽器關閉它就會從計算機的內存中被刪除。
返回頁首
參考
相關 Microsoft 知識庫“How To”文章
Q299987 HOW TO: Use Database and ASP Sessions to Implement ASP Security(使用數據庫和 ASP 會話實現 ASP 安全)
CHS299970 HOW TO: 使用 NTFS 安全保護在 IIS 4.0 或 5.0 上運行的 Web 頁
Microsoft 安全參考
Microsoft 安全
http://www.microsoft.com/security/ ;
TechNet Web 站點安全
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/itsolutions/security/Default.asp ;
如果上述 TechNet 鏈接失敗,請通過下面的地址瀏覽到 TechNet 主頁:
http://www.microsoft.com/technet/default.asp ;
在左窗格中,指向 Security(安全),然后單擊 Web Site( Web 站點)。
通用安全參考
白皮書: Implementing a Secure Site with ASP(使用 ASP 實現安全的站點)
http://www.microsoft.com/serviceproviders/whitepapers/impl_secure_asp_p10674.asp ;
Q164882Practical Recommendations for Securing Internet-Connected Windows NT Systems(關于加強通過 Internet 連接的 Windows NT 系統安全性的實用建議)
Q282060 Resources for Securing Internet Information Services(用于加強 Internet 信息服務安全性的資源)
Q271071 Minimum NTFS Permissions Required for IIS 5.0 to Work(IIS 5.0 工作所需的最低 NTFS 權限)
Q174811Authentication and Security White Paper for Internet Developers(面向 Internet 開發人員的身份驗證和安全性白皮書)
Q229694 How to Use the IIS Security "What If" Tool(如何使用 IIS 安全“What If”工具)
Q172925 INFO: Security Issues with Objects in ASP and ISAPI Extensions(ASP 和 ISAPI 擴展中對象的安全問題)
專用安全參考
Q239120 Create a Secure FTP Directory that Uses Password Authentication(創建使用密碼驗證的安全的 FTP 目錄)
Q216705 How to Set Permissions on a FrontPage Web on IIS(如何在以 IIS 為基礎的 FrontPage Web 上設置權限)
Q280383 IIS Security Recommendations When You Use a UNC Share and Username and Password Credentials(使用 UNC 共享和用戶名及密碼憑據時的 IIS 安全建議)
Q176378 HOWTO: SQL Server with Integrated Security, IIS on Same Machine(使用集成安全性的 SQL Server 和 IIS 在同一臺計算機上)
Q260985 XIMS: Minimum NTFS Permissions Required to Use CDONTS(使用 CDONTS 所需的最低 NTFS 權限)
CHS257685 Proxy Server 2.0 安全性清單
Q165340 Change Permissions Needed on Index Server System Files(更改索引服務器系統文件所需的權限)
CHS235874 Proxy Server 2.0 需要的 Windows NT 文件系統 (NTFS) 權限
返回頁首
First Published: Jun 12 2001 12:16PM
關鍵字 kbASPObj kbScript kbSecurity kbServer kbVBScript kbWebServer kbGrpDSASP kbDSupport kbCodeSnippet kbSysAdmin kbHOWTOmaster kbhowto??
Microsoft和/或其各供應商對于為任何目的而在本服務器上發布的文件及有關圖形所含信息的適用性,不作任何聲明。 所有該等文件及有關圖形均"依樣"提供,而不帶任何性質的保證。Microsoft和/或其各供應商特此聲明,對所有與該等信息有關的保證和條件不負任何責任,該等保證和條件包括關于適銷性、符合特定用途、所有權和非侵權的所有默示保證和條件。在任何情況下,在由于使用或運行本服務器上的信息所引起的或與該等使用或運行有關的訴訟中,Microsoft和/或其各供應商就因喪失使用、數據或利潤所導致的任何特別的、
概要
創建應用程序
測試應用程序
其他注意事項
疑難解答
參考
概要
本文演示如何編寫簡單的 Active Server Pages (ASP) 代碼來通過登錄頁對訪問進行限制。 本文介紹的都是一些簡化的方法。 如想獲得更多的功能或更可靠的安全性,請參見本文末尾的“參考”一節。
在本示例中,您將創建下面兩個 ASP 頁:
MyPage.asp: 此頁是受保護的,如果不提供正確的用戶名和密碼,則不能訪問此頁。
Logon.asp: 此頁提供了一個窗體,用戶可將其憑據鍵入此窗體中。 該窗體接著就會驗證用戶的用戶名和密碼。 如果用戶名和密碼正確,它就向客戶機寫入一個 Cookie,此 Cookie 就成了訪問其他 ASP 頁的“鑰匙”。
返回頁首
創建應用程序
使用“記事本”創建這些 ASP 頁。 要啟動“記事本”,請從 Windows 開始菜單上,指向程序,指向附件,然后單擊記事本。 將每個文檔保存到本地 Web 服務器的根 Web 目錄(通常是 C:/InetPub/Wwwroot/)下。 如要更改文檔的位置,則還必須對這些文件中的腳本進行相應的修改。
Logon.asp
在“記事本”中,單擊文件菜單中的新建。
選中以下代碼,右鍵單擊選中的內容,然后單擊快捷菜單中的復制。在“記事本”中,單擊編輯菜單上的粘貼以便將以下代碼粘貼到“記事本”中:
<html>
<head>
<title>Logon Form</title>
<%
Username="Administrator"
Password="Admin"
Validated = "OK"
if Strcomp(Request.Form("User"),Username,1)=0 AND Request.Form("password") = Password then
'Set the validation cookie and redirect the user to the original page.
???? Response.Cookies("ValidUser") = Validated
???? 'Check where the users are coming from within the application.
???? If (Request.QueryString("from")<>"") then
Response.Redirect Request.QueryString("from")
else
'If the first page that the user accessed is the Logon page,
???????? 'direct them to the default page.
?????????? Response.Redirect "MyPage.asp"
???? End if????
Else
' Only present the failure message if the user typed in something.
???? If Request.Form("User") <> "" then
???????? Response.Write "<h3>Authorization Failed.</h3>" & "<br>" & _
???????? "Please try again.<br> <br>"
???? End if
End if
%>
</head>
<body bgcolor="#FFFFFF">
<FORM ACTION=<%Response.Write "Logon.asp?"&Request.QueryString%> method="post">
<h3>Logon Page for MyPage.asp</h3>
<p>
Username:
<INPUT TYPE="text" NAME="User" VALUE='' size="20"></INPUT>
Password:
<INPUT TYPE="password" NAME="password" VALUE='' size="20"></INPUT>
<INPUT TYPE="submit" VALUE="Logon"></INPUT>
</FORM>
</body>
</html>
將此頁在 C:/InetPub/Wwwroot/ 文件夾中保存為 Logon.asp。
MyPage.asp
MyPage.asp 是您想要保護的頁。 您可以使用具有 .asp 文件擴展名的任何頁。
在“記事本”中,單擊文件菜單上的新建。
選中以下代碼,右鍵單擊選中的內容,然后單擊快捷菜單中的復制。在“記事本”中,單擊編輯菜單上的粘貼以便將以下代碼粘貼到“記事本”中:
<%
Validated = "OK"
if Request.Cookies("ValidUser") <> Validated then
'Construct the URL for the current page.
???? dim s
???? s = "http://";
???? s = s & Request.ServerVariables("HTTP_HOST")
???? s = s & Request.ServerVariables("URL")
???? if Request.QueryString.Count > 0 THEN
s = s & "?" & Request.QueryString
end if
???? 'Redirect unauthorized users to the logon page.
???? Response.Redirect "Logon.asp?from=" &Server.URLEncode(s)
End if
%>
<html>
<head>
<title>My Protected Page</title>
</head>
<body>
<p align="center">This is my secret information<br>
You cannot see it unless you<br>
are properly logged on!</p>
</body>
</html>
將此頁在 C:/InetPut/Wwwroot/ 文件夾中保存為 MyPage.asp。
返回頁首
測試應用程序
打開 Web 瀏覽器。 如果您使用的是 Microsoft Internet Explorer,請從 Windows 開始菜單上,指向程序,然后單擊 Internet Explorer。
在“地址”欄中鍵入下面的地址,然后按 ENTER 鍵:
http://localhost/MyPage.asp ;
您會注意到,您被重定向到 Logon.asp。
鍵入 Logon.asp 文件中 ASP 代碼中包含的用戶名和密碼信息(用戶名:Administrator,密碼:Admin),然后單擊登錄。 這樣您就應當可以看到 MyPage.asp 頁了。
請再鍵入一個錯誤的用戶名或密碼,以確認您無法登錄,因而也就不能瀏覽到 MyPage.asp。
返回頁首
其他注意事項
如想保護其他 ASP 頁,請將以下代碼添加到 ASP 頁的頂部,位于其他所有代碼之前:
<%
Validated = "OK"
if Request.Cookies("ValidUser") <> Validated then
'Construct the URL for the current page.
???? dim s
???? s = "http://";
???? s = s & Request.ServerVariables("HTTP_HOST")
???? s = s & Request.ServerVariables("URL")
???? if Request.QueryString.Count > 0 THEN
s = s & "?" & Request.QueryString
end if
???? 'Redirect unauthorized users to the logon page.
???? Response.Redirect "Logon.asp?from=" &Server.URLEncode(s)
End if
%>
若要登錄并重定向到所請求的受保護頁,必須讓超鏈接指向此頁面而非 Logon.asp 頁。 在本例中,要確保超鏈接指向 MyPage.asp。如果您未登錄,則此頁中包括的代碼將使您自動重定向到 Logon.asp。
如果確實想讓您站點的訪問者每次訪問時都登錄,則可以將 ValidUser 這一 Cookie 保存到他們的計算機上,以便他們下次訪問時能使用此信息。 一旦會話超時或您關閉瀏覽器窗口,前面的代碼就會導致 Cookie 過期。 如想設置 Cookie 的過期期限,請在 Logon.asp 中將下面的代碼
???????? Response.Cookies("ValidUser") = Validated
改為:
???????? Response.Cookies("ValidUser") = Validated
???????? Response.Cookies ("ValidUser").Expires = DATE + 1
若要指定過期期限,可將“1”改為所需的天數。 例如,下面的代碼會使 Cookie 在您的計算機上保留一年后過期:
???????? Response.Cookies ("ValidUser").Expires = DATE + 365
如果您設置了過期日期,則 Cookie 將保存到最終用戶的計算機上,這樣此用戶以后就可以繞過登錄頁。 不過,如果該用戶是從另一臺計算機上瀏覽到該站點的,則此 Cookie 就會保存到那一臺計算機上,這樣別人就有可能讀到并復制此信息。
返回頁首
缺陷
ASP 登錄頁可以用于很多應用程序,但它不能提供最高的安全性。 一般來說,NTFS 是最安全的。 NTFS 要求用戶鍵入 Microsoft Windows 可識別的用戶名和密碼。 NTFS 安全機制可用來為硬盤上的文件和文件夾設置訪問權限。
另外,ASP 安全性以 Microsoft Internet Information Server (IIS) 安全性為基礎。 如果未能妥善設置 IIS,而您添加了 ASP 安全功能,那么您不能防止高級用戶訪問您的站點。
有關 IIS 和 ASP 安全性的更多信息,請參見“參考”一節。
前面那段代碼只允許使用一組用戶憑據。 下面的 Microsoft 知識庫文章演示了如何使用一種在數據庫中保存了許多用戶名的 ASP 登錄頁:
Q299987 HOW TO: Use Database and ASP Sessions to Implement ASP Security(使用數據庫和 ASP 會話實現 ASP 安全)
如果您為 Cookie 設置了過期日期,則此 Cookie 就保存在瀏覽您的頁面時使用的那臺計算機上。 如果有人從咖啡館里的計算機等公用計算機上瀏覽您的頁面,那么 Cookie 就保存在此計算機上,于是其他人就有可能讀到并復制此信息。 如果不設置過期日期,則 Cookie 就不會保存到計算機的硬盤上(它只存儲在內存中),而且一旦瀏覽器關閉它就會從計算機的內存中被刪除。
返回頁首
參考
相關 Microsoft 知識庫“How To”文章
Q299987 HOW TO: Use Database and ASP Sessions to Implement ASP Security(使用數據庫和 ASP 會話實現 ASP 安全)
CHS299970 HOW TO: 使用 NTFS 安全保護在 IIS 4.0 或 5.0 上運行的 Web 頁
Microsoft 安全參考
Microsoft 安全
http://www.microsoft.com/security/ ;
TechNet Web 站點安全
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/itsolutions/security/Default.asp ;
如果上述 TechNet 鏈接失敗,請通過下面的地址瀏覽到 TechNet 主頁:
http://www.microsoft.com/technet/default.asp ;
在左窗格中,指向 Security(安全),然后單擊 Web Site( Web 站點)。
通用安全參考
白皮書: Implementing a Secure Site with ASP(使用 ASP 實現安全的站點)
http://www.microsoft.com/serviceproviders/whitepapers/impl_secure_asp_p10674.asp ;
Q164882Practical Recommendations for Securing Internet-Connected Windows NT Systems(關于加強通過 Internet 連接的 Windows NT 系統安全性的實用建議)
Q282060 Resources for Securing Internet Information Services(用于加強 Internet 信息服務安全性的資源)
Q271071 Minimum NTFS Permissions Required for IIS 5.0 to Work(IIS 5.0 工作所需的最低 NTFS 權限)
Q174811Authentication and Security White Paper for Internet Developers(面向 Internet 開發人員的身份驗證和安全性白皮書)
Q229694 How to Use the IIS Security "What If" Tool(如何使用 IIS 安全“What If”工具)
Q172925 INFO: Security Issues with Objects in ASP and ISAPI Extensions(ASP 和 ISAPI 擴展中對象的安全問題)
專用安全參考
Q239120 Create a Secure FTP Directory that Uses Password Authentication(創建使用密碼驗證的安全的 FTP 目錄)
Q216705 How to Set Permissions on a FrontPage Web on IIS(如何在以 IIS 為基礎的 FrontPage Web 上設置權限)
Q280383 IIS Security Recommendations When You Use a UNC Share and Username and Password Credentials(使用 UNC 共享和用戶名及密碼憑據時的 IIS 安全建議)
Q176378 HOWTO: SQL Server with Integrated Security, IIS on Same Machine(使用集成安全性的 SQL Server 和 IIS 在同一臺計算機上)
Q260985 XIMS: Minimum NTFS Permissions Required to Use CDONTS(使用 CDONTS 所需的最低 NTFS 權限)
CHS257685 Proxy Server 2.0 安全性清單
Q165340 Change Permissions Needed on Index Server System Files(更改索引服務器系統文件所需的權限)
CHS235874 Proxy Server 2.0 需要的 Windows NT 文件系統 (NTFS) 權限
返回頁首
First Published: Jun 12 2001 12:16PM
關鍵字 kbASPObj kbScript kbSecurity kbServer kbVBScript kbWebServer kbGrpDSASP kbDSupport kbCodeSnippet kbSysAdmin kbHOWTOmaster kbhowto??
Microsoft和/或其各供應商對于為任何目的而在本服務器上發布的文件及有關圖形所含信息的適用性,不作任何聲明。 所有該等文件及有關圖形均"依樣"提供,而不帶任何性質的保證。Microsoft和/或其各供應商特此聲明,對所有與該等信息有關的保證和條件不負任何責任,該等保證和條件包括關于適銷性、符合特定用途、所有權和非侵權的所有默示保證和條件。在任何情況下,在由于使用或運行本服務器上的信息所引起的或與該等使用或運行有關的訴訟中,Microsoft和/或其各供應商就因喪失使用、數據或利潤所導致的任何特別的、
?
總結
以上是生活随笔為你收集整理的ASP 代码给 ASP 页加密码保护的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp获取屏幕分辨率
- 下一篇: 记录密码的asp代码