I'm making use of a captcha generator. http://xcaptcha.codeplex.com/. In the example it shows using Session.Add
for comparing captcha generated vs answer. I wanted to make this a part of my model's rule violation checker but I can't seem to find/work with the correct parts of session there. Am I missing the way to navigate to session from within my model's GetRuleViolations? Any other tips appreciated
// Controller
public ActionResult Image()
{
var builder = new XCaptcha.ImageBuilder();
var result = builder.Create();
Session.Add("CaptchaKey", result.Solution);
return new FileContentResult(result.Image, result.ContentType);
}
// My model
public IEnumerable<RuleViolation> GetRuleViolations()
{
// This isn't correct... it isn't stored in server variables
string captchaAnswer = HttpContext.Current.Request.ServerVariables.Get("CaptchaKey");
// No such thing..
string res = Session["Something"].ToString();
}
// My eh solution
// Controller
[HttpPost]
public ActionResult ContactUs(EmailModel e, FormCollection collection)
{
string captchaAnswer = Session["CaptchaKey"].ToString();
string captchaText = collection["CaptchaText"].ToUpper().ToString();
bool correctCaptcha = (captchaAnswer.CompareTo(captchaText) == 0) ? true : false;
if (e.IsValid && correctCaptcha)
{
EmailHelper.SendMessage(e);
return RedirectToAction("ContactSuccess");
}
else
{
if (!correctCaptcha)
ModelState.AddModelError("Captcha", "Incorrect Captcha answer");
ModelState.AddRuleViolations(e.GetRuleViolations());
}
return View(e);
}