手写验证码
手写验证码
Html:
<div class="field"> <input type="text" class="input input-big" name="code" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码" /> <img src="/Login/ShowVCode" id="LoginCode" alt="" width="100" height="32" class="passcode" style="height:43px;cursor:pointer;" onclick="this.src=this.src+'?'"> </div> </div>
控制器:
#region 验证码生成 public ActionResult ShowVCode() { ValidateCode validateCode = new ValidateCode(); string strCode = validateCode.CreateValidateCode(4); Session["VCode"] = strCode; byte[] imgBytes = validateCode.CreateValidateGraphic(strCode); return File(imgBytes, @"image/jpeg"); } #endregion
在控制器上怎么接受到验证码:
#region 验证 public ActionResult Logincode() { string strCode = Request["code"];//接收到的Code string sessionCode = Session["VCode"] as string;//生成的Code Session["VCode"] = null; if (string.IsNullOrEmpty(sessionCode)) { //return Content("验证码失败"); return Redirect("Index"); } if (strCode == sessionCode) { // return Content("验证码正确"); // return Redirect("/Shopper/Index"); return RedirectToAction("Index", "Shopper", new { name = UserName }); } else { //return Content("验证码失败"); return Redirect("Index"); } } #endregion