How to use it
1. add handler tag in web.config:
<add verb="GET" path="/captcha/" type="PersianCaptchaHandler.CaptchaHandler, PersianCaptchaHandler, Version=1.0.0.0, Culture=neutral" />
2. drag an image control and in behind code ad some code to set src attribute:
for example add these codes in page load event:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
SetCaptcha();
}

private void SetCaptcha()
{
lblMessage.Text =
txtCaptcha.Text = string.Empty;

var newNumber =
RandomGenerator.Next(100, 999)
;

var farsiAlphabatic = NumberToString.ConvertIntNumberToFarsiAlphabatic(newNumber.ToString());

hfCaptchaText.Value =
HttpUtility
.UrlEncode(
Encryptor.Encrypt(
farsiAlphabatic
)
);

txtCaptcha.Text = string.Empty;
imgCaptchaText.ImageUrl =
"/captcha/?text=" + hfCaptchaText.Value;
}
3. after user entered captcha letters in number fo validating inputs:
private string GetCaptcha()
{
var farsiAlphabatic = NumberToString.ConvertIntNumberToFarsiAlphabatic(txtCaptcha.Text);

var encryptedString =
HttpUtility
.UrlEncode(
Encryptor.Encrypt(
farsiAlphabatic
)
);

return encryptedString;
}

private bool ValidateUserInputForLogin()
{
if (!Utils.IsNumber(txtCaptcha.Text))
{
// number excepted error
return false;
}

var strGetCaptcha =
GetCaptcha();

var strDecodedVAlue =
hfCaptchaText.Value;

if (strDecodedVAlue != strGetCaptcha)
{
// wrong input error
SetCaptcha();
return false;
}
return true;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!ValidateUserInputForLogin()) return;
// continue to process
}

protected void btnRefreshCaptcha_Click(object sender, ImageClickEventArgs e)
{
SetCaptcha();
}

Last edited Nov 18, 2012 at 8:47 AM by payervand, version 1

Comments

No comments yet.