我正在使用验证码生成器。 http://xcaptcha.codeplex.com/。在示例中,它显示使用Session.Add
来比较验证码生成与答案。我想让它成为我的模型的规则违规检查器的一部分,但我似乎无法找到/使用那里的会话的正确部分。我是否错过了从模型的GetRuleViolations中导航到会话的方法?任何其他提示赞赏
// 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);
}
答案 0 :(得分:2)
HttpContext.Current.Session
答案 1 :(得分:1)
我刚刚更新了它。现在有一个版本2,它不再使用会话来维护状态并使用基于模型的验证。