Hướng dẫn sử dụng BotDetect ASP.NET CAPTCHA trên máy chủ Linux/Apache
Trang này hướng dẫn bạn cách dùng BotDetect ASP.NET CAPTCHA để bảo vệ ứng dụng web ASP.NET dùng máy chủ Apache trên hệ thống Linux (Red Hat, Fedora, CentOS, SUSE, ...).
Yêu cầu
- Apache 1.3.x/2.0.x/2.2.x
- BotDetect CAPTCHA 2.0.x cho ASP.NET
Giả định rằng bạn đã cài đặt máy chủ Apache trên hệ thống của bạn. Nếu chưa, xin hãy tải về Apache, và theo hướng dẫn cài đặt Apache.
Hiện tại, gói cài đặt BotDetect CAPTCHA chỉ có thể chạy trên hệ thống Windows, nhưng bạn có thể copy file (Lanap.BotDetect.dll) và dự án mẫu ASP.NET vào hệ thống Linux. Nếu bạn không có gói cài đặt BotDetect CAPTCHA, xin hãy tải về BotDetect CAPTCHA phiên bản dùng thử miễn phí.
Hướng dẫn từng bước
Hướng dẫn này sẽ giúp bạn từng bước cần thiết để tạo trang ASP.NET và sử dụng BotDetect CAPTCHA để bảo vệ nó. Nếu bạn đã có sẵn trang web và đã được cấu hình, bạn có thể bỏ qua những bước không cần thiết.
- Bước 1. Cài đặt Mono framework
- Bước 2. Tạo mới website ASP.NET
- Bước 3. Cấu hình website để sử dụng BotDetect CAPTCHA
- Bước 4. Thêm xử lý xác thực người dùng
- Dự án mẫu ASP.NET BotDetect CAPTCHA
Bước 1. Cài đặt Mono framework
Chúng ta sẽ sử dụng Mono framework để chạy ứng dụng web .NET trên hệ thống Linux.
Mod_Mono là một Apache 1.3/2.0/2.2 module hỗ trợ ASP.NET cho máy chủ web Apache. Module này gửi yêu cầu trang ASP.NET cho một chương trình thứ ba, mod-mono-server, để xử lý. Việc giao tiếp giữa Apache module và mod-mono-server được thiết lập sử dụng Unix socket hoặc TCP socket.
- Mono Project
Cài đặt Mono-core
Tải về gói Mono-core phù hợp với hệ thống của bạn từ http://www.go-mono.com/mono-downloads/download.html. Xin hãy cài đặt các gói sau theo đúng thứ tự sau:
- giflib-4.1.3-8.i386.rpm
- libexif9-0.5.12-3mdk.i586.rpm
- libgdiplus0-1.9-1.rhel4.novell.i386.rpm
- mono-core-1.9.1-2.novell.i586.rpm
- mono-data-1.9.1-2.novell.i586.rpm
- mono-data-firebird-1.9.1-2.novell.i586.rpm
- mono-data-oracle-1.9.1-2.novell.i586.rpm
- mono-data-postgresql-1.9.1-2.novell.i586.rpm
- mono-data-sqlite-1.9.1-2.novell.i586.rpm
- mono-data-sybase-1.9.1-2.novell.i586.rpm
- ibm-data-db2-1.9.1-2.novell.i586.rpm
- mono-nunit-1.9.1-2.novell.i586.rpm
- mono-web-1.9.1-2.novell.i586.rpm
- mono-winforms-1.9.1-2.novell.i586.rpm
- bytefx-data-mysql-1.9.1-2.novell.i586.rpm
- xsp-1.9.1-0.novell.noarch.rpm
Cài đặt Mod_Mono
Xin hãy cài đặt Mod_Mono từ gói mã nguồn, để bạn có thể chỉ định phiên bản Apache bạn muốn dùng. Nhiều gói RPM (ví dụ, mod_mono-1.9-0.rhel4.novell.i386.rpm) đã được biên dịch cho một phiên bản Apache nhất định, nên bạn cũng có thể dùng gói đã biên dịch nếu phù hợp với phiên bản Apache của bạn.
Tải về mã nguồn của Mod_Mono (mod_mono-1.9.tar.bz2) từ http://ftp.novell.com/pub/mono/sources-stable/, và theo hướng dẫn biên dịch Mod_Mono từ mã nguồn.
Thiết lập Apache để dùng Mod_Mono
Thêm các dòng sau vào file httpd.conf (mặc định nó nằm trong thư mục /usr/local/apache2/conf):
Include conf/mod_mono.conf <Location /mono> SetHandler mono-ctrl Order deny,allow Deny from all Allow from 127.0.0.1 </Location>
Dòng đầu tiên sẽ load Mod_Mono module. Chắc chắn rằng bạn có file mod_mono.conf trong cùng thư mục với httpd.conf sau khi cài đặt Mod_Mono.
Những dòng còn lại (tuỳ chọn) sẽ thêm Mod_Mono control panel để bạn có thể quản lý Mod_Mono sử dụng http://localhost/mono.
Bước 2. Tạo website ASP.NET
Tạo thư mục mới cho website (chúng tôi sử dụng thư mục BotDetectSample trong hướng dẫn này), trong thư mục web của Apache (mặc định là /usr/local/apache2/htdocs/)
Tạo file mới tên là Default.aspx trong thư mục dự án, với nội dung sau:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/
xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>BotDetect Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div id="PromptDiv">
<span id="Prompt">Type the characters you see in
the picture</span>
</div>
</form>
</body>
</html>
Tạo file mới tên là Default.aspx.cs trong thư mục dự án, với nội dung sau:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
}
Thêm các dòng sau vào file httpd.conf (nằm trong thư mục /usr/local/apache2/conf/):
Alias /BotDetectSample "/usr/local/apache2/htdocs/BotDetectSample"
AddMonoApplications default "/BotDetectSample:/usr/local/apache2/
htdocs/BotDetectSample"
MonoServerPath /usr/bin/mod-mono-server2
<Location /BotDetectSample>
SetHandler mono
</Location>
Khởi động lại Apache bằng lệnh:
$httpd –k restart
...và nếu mọi thứ đều được thiết lập đúng, bạn có thể thấy trang web trên trình duyệt khi bạn vào http://localhost/BotDetectSample/
Bước 3. Cấu hình trang web để sử dụng BotDetect CAPTCHA
Tạo thư mục con tên Bin trong thư mục dự án
Copy file Lanap.BotDetect.dll vào thư mục bin vừa mới tạo
Tạo file mới tên Web.config trong thư mục dự án với nội dung sau:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<httpHandlers>
<add verb="*" path="LanapCaptcha.aspx"
type="Lanap.BotDetect.CaptchaHandler,
Lanap.BotDetect"/>
</httpHandlers>
<sessionState mode="InProc" cookieless="AutoDetect"
timeout="20" sessionIDManagerType="
Lanap.BotDetect.Persistence.CustomSessionIDManager,
Lanap.BotDetect" />
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="false">
<assemblies>
<add assembly="System.Design, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="None"/>
</system.web>
</configuration>
Thêm các dòng sau đây vào phần đầu của file Default.aspx, chỉ sau thẻ <%@Page %>:
<%@ Register Assembly="Lanap.BotDetect" Namespace="Lanap.BotDetect" TagPrefix="BotDetect" %>
Trong cùng file đó, thêm dòng sau vào mã nguồn của trang:
<div id="CaptchaDiv"> <BotDetect:Captcha ID="SampleCaptcha" runat="server" /> </div>
Lưu tất cả thay đổi, và tải lại trang trên trình duyệt. Bạn sẽ thấy hình ảnh CAPTCHA hiện thị trên trang web.
Bước 4. Thêm phần xử lý xác thực người dùng
Trong file Default.aspx, thêm đoạn mã sau vào trang:
<div id="ValidationDiv">
<asp:TextBox ID="CodeTextBox" runat="server">
</asp:TextBox>
<asp:Button ID="ValidateButton" runat="server"
Text="Validate" />
<asp:Label ID="MessageCorrectLabel" runat="server">
</asp:Label>
<asp:Label ID="MessageIncorrectLabel" runat="server">
</asp:Label>
</div>
Trong file Default.aspx.cs, thêm đoạn mã sau vào phần định nghĩa lớp:
protected void Page_PreRender(object sender, EventArgs e)
{
/// initial page setup
if (!IsPostBack)
{
/// set control text
ValidateButton.Text = "Validate";
MessageCorrectLabel.Text = "Correct!";
MessageIncorrectLabel.Text = "Incorrect!";
/// these messages are shown only after validation
MessageCorrectLabel.Visible = false;
MessageIncorrectLabel.Visible = false;
}
if (IsPostBack)
{
/// validate the input code, and show the appropriate
/// message
string code = CodeTextBox.Text.Trim().ToUpper();
if (SampleCaptcha.Validate(code))
{
MessageCorrectLabel.Visible = true;
MessageIncorrectLabel.Visible = false;
}
else
{
MessageCorrectLabel.Visible = false;
MessageIncorrectLabel.Visible = true;
}
CodeTextBox.Text = null;
}
}
Lưu tất cả thay đổi, và tải lại trang trên trình duyệt. Bạn có thể thử hoạt động của xác thực CAPTCHA.
Trên trang web chính thức bạn sẽ thay đổi mã nguồn xác thực để chuyển người dùng về trang yêu cầu khi xác thực CAPTCHA thành công.
Mã nguồn dự án mẫu của BotDetect CAPTCHA
Bạn có thể tìm thấy mã nguồn đầy đủ của hướng dẫn này tại mã nguồn dự án mẫu đi kèm với gói cài đặt BotDetect CAPTCHA. Bạn có thể có kết quả tương tự như hướng dẫn trên bằng cách copy Ví dụ xác thực BotDetect CAPTCHA vào máy chủ Linux và phát hành nó trên Apache sử dụng Mod_Mono theo như đã hướng dẫn.
Phiên bản hiện tại của BotDetect
- BotDetect ASP.NET CAPTCHA v2.0.152009–11–23
- BotDetect ASP CAPTCHA v2.0.92009–02–12
Xin lưu ý
Trang này là bản dịch tiếng Việt không chính thức của trang gốc tiếng Anh: How To use BotDetect ASP.NET CAPTCHA on Linux/Apache servers và có thể không chính xác, không đầy đủ hoặc không cập nhật.
Cập nhật ngày 2009-11-30. Áp dụng cho BotDetect ASP.NET CAPTCHA v2.0.15 và BotDetect ASP CAPTCHA v2.0.9.



















