Wednesday, 8 April 2015

captcha mvc4



Install-Package CaptchaMvc.Mvc4

[CaptchaMvc.Attributes.CaptchaVerify("Captcha is not valid")]
        [HttpPost]
        public ActionResult Contact(ContactDto model)
        {
            if (ModelState.IsValid)
            {
                var saved = _repository.SaveContact(model);
                var msg = saved ? "Your details are saved." : "Error while saving details.";
                ModelState.AddModelError("", msg);
                return View();
            }
            else
            {
                return View();
            }
        }


@using CaptchaMvc.HtmlHelpers
....code....
<br />@Html.Captcha(3)
<input type="submit" value="Send">

Saturday, 4 April 2015

Captcha Mvc



Install-Package CaptchaMvc.Mvc5

using System.Web.Mvc;
using CaptchaMvc.HtmlHelpers;

namespace MvcCaptcha.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index() { return View(); }

        public ActionResult Captcha() { return View(); }

        [CaptchaMvc.Attributes.CaptchaVerify("Captcha is not valid")]
        [HttpPost]
        public ActionResult Captcha(string empty)
        {
            if (ModelState.IsValid)
            {
                ViewBag.ErrMessage = "Message: captcha is valid.";
                return View();
            }
            ViewBag.ErrMessage = "Error: captcha is not valid.";
            return View();
        }

        public ActionResult MathCaptcha() { return View(); }

        [HttpPost]
        public ActionResult MathCaptcha(string empty)
        {
            if (this.IsCaptchaValid("Captcha is not valid"))
            {
                ViewBag.ErrMessage = "Message: captcha is valid.";
                return View();
            }
            ViewBag.ErrMessage = "Error: captcha is not valid.";
            return View();
        }
    }
}

@using CaptchaMvc.HtmlHelpers

@model MvcCaptcha.Models.Feedback 
@{ ViewBag.Title = "Word Captcha"; }

<style>.Error{color: red}.input-validation-error{border: 1px solid #ff0000;background-color: #ffeeee;}</style>
<h2>Word Captcha</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Feedback</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Title, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Comment)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Comment, 3, 20, new{@class="form-control"})
            @Html.ValidationMessageFor(model => model.Comment)
        </div>

        @*@Html.MathCaptcha()*@
        @Html.Captcha(3)

        <br />
        <p class="Error">  @ViewBag.ErrMessage </p>
        <p> <input type="submit" value="Send" /> </p>
    </fieldset>
}