How to implement Google reCaptcha in MVC application without API


In this post, I am explain how to implement Google reCaptcha in MVC application without API.

Google reCAPTCHA  is a free service from Google that helps protect websites from spam and abuse that restricts the automated input sent by a system and allows only input from a real human.

We can create a CAPTCHA in many ways but Google provides a free reCAPTCHA with better security without any cost.

Let us learn about  how to implement Google reCaptcha in MVC application without API.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Sign up & Generate Key for Google reCaptcha.

Go to > Sign up for Google reCaptcha > Create Key(for Google reCaptcha).

Step-3: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-4: Create table for Save Data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-5: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-6: Create a Controller .

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Here I have created a controller "ContactController"

Step-7: Add new action into your controller for Get Method

Here I have added "Query" Action into "Contact" Controller. Please write this following code

            namespace MVCGoogleReCaptcha.Controllers
                public class ContactController : Controller
                    public ActionResult Query()
                        return View();

[N:B: Rebuild your application here before Add View]

Step-8: Add view for your Action & design for create form.

Right Click on Action Method (here right click on Query action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Select Scaffold templete "Create" > Add.
HTML Code (View)
            @model MVCGoogleReCaptcha.QueryData

                ViewBag.Title = "Query";


            @using (Html.BeginForm()) {
                if (ViewBag.Message != null)
                    <div style="font-weight:bold; color:red; border:1px solid black">
                    <legend>Query Data</legend>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.FullName)
                    <div class="editor-field">
                        @Html.EditorFor(model => model.FullName)
                        @Html.ValidationMessageFor(model => model.FullName)

                    <div class="editor-label">
                        @Html.LabelFor(model => model.EmailID)
                    <div class="editor-field">
                        @Html.EditorFor(model => model.EmailID)
                        @Html.ValidationMessageFor(model => model.EmailID)

                    <div class="editor-label">
                        @Html.LabelFor(model => model.ContactNo)
                    <div class="editor-field">
                        @Html.EditorFor(model => model.ContactNo)
                        @Html.ValidationMessageFor(model => model.ContactNo)

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Query)
                    <div class="editor-field">
                        @Html.EditorFor(model => model.Query)
                        @Html.ValidationMessageFor(model => model.Query)
                    @* I Will Place Google Re-Captcha Here *@
                    <div class="editor-label">
                        Security Code
                    <div class="editor-label">
                        @* You Can get the Script code from *@
                        <script type="text/javascript"
                            <iframe src=""
                                height="300" width="500" frameborder="0"></iframe><br>
                            <textarea name="recaptcha_challenge_field" rows="3" cols="40">
                            <input type="hidden" name="recaptcha_response_field"
                        <input type="submit" value="Create" />

                @Html.ActionLink("Back to List", "Index")

            @section Scripts {

Step-9: Add new action into your controller for POST Method

Here I have added "Query" Action with Model Parameter (here "Contact") into "Contact" Controller. Please write this following code

            public ActionResult Query(QueryData qd)
                if (ModelState.IsValid)
                    // Here I will check is captcha code is valid or not
                    bool isCaptchaCodeValid = false;
                    string CaptchaMessage = "";
                    isCaptchaCodeValid = GetCaptchaResponse(out CaptchaMessage);

                    if (isCaptchaCodeValid)
                        // Save Data Here
                        using (MyDatabaseEntities dc = new MyDatabaseEntities())
                            qd = null;
                            ViewBag.Message = "Query Saved Successfully";
                        ViewBag.Message = CaptchaMessage;
                return View(qd);

Here is the function for get response from google api about validation of captcha code.

            private bool GetCaptchaResponse(out string message)
                bool flag = false;
                message = "";

                string[] result;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
                request.ProtocolVersion = HttpVersion.Version10;
                request.Timeout = 0x7530;
                request.Method = "POST";
                request.UserAgent = "reCAPTCHA/ASP.NET";
                request.ContentType = "application/x-www-form-urlencoded";
                string formData = string.Format(
                    new object[]{
                byte[] formbytes= Encoding.ASCII.GetBytes(formData);

                using (System.IO.Stream requestStream = request.GetRequestStream())
                    requestStream.Write(formbytes, 0, formbytes.Length);


                    using (WebResponse httpResponse = request.GetResponse())
                        using (System.IO.TextReader readStream = new System.IO.StreamReader(httpResponse.GetResponseStream(),Encoding.UTF8))
                            result = readStream.ReadToEnd().Split(new string[] { "\n", @"\n" }, StringSplitOptions.RemoveEmptyEntries);
                            message = result[1];
                            flag = Convert.ToBoolean(result[0]);
                catch (Exception ex)
                    message = ex.Message;
                    return false;
                return flag;

Step-10: Run Application.

Related post :

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms,, sql server, entity framework, Ajax, Jquery, web api, web service and more.