Hi yogeshc,
Follow the below steps and check.
Add the CaptchaMvc (CaptchaMvc.Mvc4) from nuget in your project.
1. Right click in the project -> Add -> Area
2. Give suitable name (Security) -> Add.
3. Add Controller in the Controllers folder and write the following code.
Namespaces
using CaptchaMvc.HtmlHelpers;
Controller
public class LoginController : Controller
{
// GET: Security/Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Login()
{
if (!this.IsCaptchaValid(""))
{
ViewBag.error = "captcha is not valid";
}
else
{
}
return View("Index");
}
}
5. Add View for the controller and write the following code.
@using CaptchaMvc.HtmlHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Login", "Login", new { area = "Security" }, FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.MathCaptcha()
<button type="submit">Login</button>
}
</body>
</html>
6. Inside the Areas folder go to Security folder and add the system.webServer section if not present.
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
Web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="Captcha_MVC" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
7. RegisterAllAreas in the global.asax file Application_Start.
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
Screenshot