|
using System;
using SecureCaptcha.Security.Cryptography;
public delegate void CaptchaEventHandler();
public partial class CaptchaControl : System.Web.UI.UserControl
{
private string color = "#ffffff";
protected string style;
private event CaptchaEventHandler success;
private event CaptchaEventHandler failure;
public string Message
{
// We don't set message in page_load, because it prevents us from changing message in failure event
set { lblCMessage.Text = value; }
get { return lblCMessage.Text; }
}
public string BackgroundColor
{
set { color = value.Trim("#".ToCharArray()); }
get { return color; }
}
public string Style
{
set { style = value; }
get { return style; }
}
public event CaptchaEventHandler Success
{
add { success += value; }
remove { success += null; }
}
public event CaptchaEventHandler Failure
{
add { failure += value; }
remove { failure += null; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetCaptcha();
txtCaptcha.Text = "";
}
}
private void SetCaptcha()
{
// Set image
string s = RandomText.Generate();
// Encrypt
string ens = Encryptor.Encrypt(s, "srgerg$%^bg", Convert.FromBase64String("srfjuoxp"));
// Save to session
Session["captcha"] = s;
// Set url
imgCaptcha.ImageUrl = "~/Captcha.ashx?w=305&h=92&c=" + ens + "&bc=" + color;
// Query strings: c for code(encrypted text), w for image width, h for image height, bc for
// Background color
}
protected void btnSubmit_Click(object s, EventArgs e)
{
if (Session["captcha"] != null && txtCaptcha.Text == Session["captcha"].ToString())
{
if (success != null)
{
success();
}
}
else
{
txtCaptcha.Text = "";
SetCaptcha();
if (failure != null)
{
failure();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.