Hướng dẫn sử dụng BotDetect ASP.NET CAPTCHA không dùng Visual Studio
Hướng dẫn này mô tả cách dùng BotDetect ASP.NET CAPTCHA trong một dự án mới ASP.NET 2.0 mà không dùng Visual Studio.
Yêu cầu
- IIS 5.0+
- .Net Framework 2.0
- BotDetect 2.0.x cho ASP.NET 2.0
Bước 1. Tạo mới ASP.NET 2.0 Website
- 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)
- 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 { }
- Click phải vào thư mục rồi chọn Properties...
- ...click "Share this folder" trong tab "Web Sharing" ...
- ...và click "OK".
- Mở "Administrative Tools" trong Control Panel...
- ...chọn "Internet Information Services"...
- ...chọn thư mục ảo mới tạo ra trong cây thư mục Website...
- ...Click phải vào thư mục rồi chọn Properties...
- ...click "Edit" trên tab "Directory Security"...
- ...đánh dấu "Anonymous access" và click "OK"...
- ...chuyển sang tab "ASP.NET" và chọn runtime version là (2.0.50727); click "Apply" để lưu mọi thay đổi...
- ...đóng hộp thoại Properties, clicl phải vào file Default.aspx và chọn "Browse"...
- ...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
Bước 2. 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
- Tìm tới file Lanap.BotDetect.dll nằm trong thư mục cài đặt của BotDetect CAPTCHA
- Copy file này vào thư mục Bin
- 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> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <handlers> <remove name="LanapCaptchaHandler" /> <add name="LanapCaptchaHandler" preCondition="integratedMode" verb="*" path="LanapCaptcha.aspx" type="Lanap.BotDetect.CaptchaHandler, Lanap.BotDetect" /> </handlers> </system.webServer> </configuration>
Bước 3. Thêm BotDetect CAPTCHA vào trang
- 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 dự án mẫu đi kèm với gói cài đặt BotDetect CAPTCHA.
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 without Visual Studio 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.





























