ASP.NET 驗証碼 Authentication Code Control – MSCaptcha 使用方法
將 Download 返來既 Package 上面 ( MSCaptcha.dll – 44kb / MSCaptcha.xml – 9kb )
抄去 project 上既 bin folder 上 及加去 Project 既 Reference 上面
web.config |
<configuration> <system.web> … … <httpHandlers> <add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/> </httpHandlers> </system.web> </configuration> |
WebForm2.aspx |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %> <%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %> <form id="form1" runat="server"> <div> <cc1:CaptchaControl ID="VerifyControl" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="6" CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240" /> <asp:TextBox ID="VerifyTextBox" runat="server" MaxLength="6"></asp:TextBox> <asp:Button ID="Verifybtn" runat="server" Text="Verify" onclick="Verifybtn_Click" /> <asp:Label ID="Verifylbl" runat="server" Text="" /> </div> </form> |
WebForm2.aspx.cs |
using System; using MSCaptcha; namespace WebApplication1 { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Verifybtn_Click(object sender, EventArgs e) { if (Page.IsPostBack) { VerifyControl.ValidateCaptcha(VerifyTextBox.Text.Trim()); if (VerifyControl.UserValidated) { Verifylbl.ForeColor = System.Drawing.Color.Green; Verifylbl.Text = "Valid"; } else { Verifylbl.ForeColor = System.Drawing.Color.Red; Verifylbl.Text = "InValid"; } } } } } |