Hi,
I am trying to implement Kendo Captcha. I tried to implement same way as in this article.
ASP.NET MVC Captcha Component Validation - Telerik UI for ASP.NET MVC
But I am getting below error. what am I doing wrong?
My code is like this
@(Html.Kendo().Captcha()
.Name("Captcha")
.CaptchaImage((string)ViewData["Captcha"])
.CaptchaId((string)ViewData["CaptchaID"])
.DataCaptchaField("Captcha") // The field containing the Captcha from the server-side should be called Captcha.
.DataCaptchaIdField("CaptchaID") // The field containing the Captcha's ID from the server-side should be called CaptchaID.
.Handler(handler => handler.Action("Reset", "IUA"))
.ValidationHandler(handler => handler.Action("Validate", "IUA"))
.ValidateOnBlur(true)
)namespace Portal.Controllers
{
public class IuaController : Controller
{
// GET: Iua
public ActionResult Index ( string pin )
{
var model = portalServices.getExistingData();
if (string.IsNullOrEmpty(model.CaptchaID))
{
GenerateNewCaptcha();
}
else
{
CaptchaImage captchaImage = (CaptchaImage)Session["captcha" + model.CaptchaID];
if (captchaImage != null && CaptchaHelper.Validate(captchaImage, model.Captcha.ToUpperInvariant()))
{
ModelState.Clear();
GenerateNewCaptcha();
}
}
//return View();
return View(model);
}
private void GenerateNewCaptcha()
{
CaptchaImage captchaImage = CaptchaHelper.GetNewCaptcha();
Session["captcha" + captchaImage.UniqueId] = captchaImage;
ViewData["Captcha"] = GetCatchaImage(captchaImage.UniqueId);//Url.Action("image", "IUA", new { captchaId = captchaImage.UniqueId });
ViewData["CaptchaID"] = captchaImage.UniqueId;
}
public ActionResult Reset()
{
CaptchaImage newCaptcha = CaptchaHelper.GetNewCaptcha();
Session["captcha" + newCaptcha.UniqueId] = newCaptcha;
return Json(new
{
captcha = GetCatchaImage(newCaptcha.UniqueId),//Url.Action("image", "IUA", new { captchaId = newCaptcha.UniqueId }),
captchaId = newCaptcha.UniqueId
}, JsonRequestBehavior.AllowGet);
}
public ActionResult Image(string captchaId)
{
CaptchaImage captcha = (CaptchaImage)Session["captcha" + captchaId];
var image = CaptchaHelper.RenderCaptcha(captcha);
byte[] bmpBytes;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
bmpBytes = ms.ToArray();
}
return File(bmpBytes, "image/png");
}
public ActionResult GetCatchaImage(string captchaId)
{
CaptchaImage captcha = (CaptchaImage)Session["captcha" + captchaId];
var image = CaptchaHelper.RenderCaptcha(captcha);
byte[] bmpBytes;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
bmpBytes = ms.ToArray();
}
return File(bmpBytes, "image/png");
}
public ActionResult Audio(string captchaId)
{
CaptchaImage captcha = (CaptchaImage)Session["captcha" + captchaId];
byte[] bmpBytes;
using (MemoryStream audio = CaptchaHelper.SpeakText(captcha))
{
bmpBytes = audio.ToArray();
}
return File(bmpBytes, "audio/wav");
}
public ActionResult Validate(string captchaId, string captcha)
{
CaptchaImage captchaImage = (CaptchaImage)Session["captcha" + captchaId];
return Json(CaptchaHelper.Validate(captchaImage, captcha.ToUpperInvariant()), JsonRequestBehavior.AllowGet);
}
}
}
Model class
public class IuaModel
{
[Key]
public string Pin { get; set; }
[Display(Name = "Ticket #")]
public long Id { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Middle Initial")]
public string MiddleInitial { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Facility Name")]
public string FacilityName { get; set; }
private string _captchaValue;
public string Captcha
{
get
{
return string.IsNullOrEmpty(_captchaValue) ? "" : _captchaValue;
}
set
{
_captchaValue = value;
}
}
public string CaptchaID { get; set; }
}anyone know what the mistake I am doing?
I tried all the ways i know but no luck . can anyone help me in this issue?
