connectseekers.com

Saturday, 4 March 2017

Captcha Code In MVC C#

3/04/2017 - By Pranav Singh 0

This article will show how you can create a custom captcha code in your mvc application using c#.net.

So for this article first we will create a new mvc application and add a controller file named as CaptchaController. Now add the below code into the controller class file.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_Demos.Controllers
{
    public class CaptchaController : Controller
    {
        //
        // GET: /Captcha/

        public ActionResult CaptchaIndex()
        {
            string[] strArray = new string[36];
            strArray = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

            Random autoRand = new Random();
            string strCaptcha = string.Empty;
            for (int i = 0; i < 6; i++)
            {
                int j = Convert.ToInt32(autoRand.Next(0, 62));
                strCaptcha += strArray[j].ToString();
            }
            Session["Captcha"] = strCaptcha;
            ImageConverter converter = new ImageConverter();
            Response.BinaryWrite((byte[])converter.ConvertTo(CaptchaGeneration(strCaptcha), typeof(byte[])));
            return View();
        }
        public Bitmap CaptchaGeneration(string captchatxt)
        {
            Bitmap bmp = new Bitmap(133, 48);
            using (Graphics graphics = Graphics.FromImage(bmp))
            {
                Font font = new Font("Tahoma", 14);
                graphics.FillRectangle(new SolidBrush(Color.Gray), 0, 0, bmp.Width, bmp.Height);
                graphics.DrawString(captchatxt, font, new SolidBrush(Color.Gold), 25, 10);
                graphics.Flush();
                font.Dispose();
                graphics.Dispose();
            }
            return bmp;
        }

    }
}

In above code I have create the bit map and of the image and saved the captcha value in the session.   This session value we will use to validate the entered captcha code.

After this we will create the view for the captcha class controller get method.

@{
    ViewBag.Title = "CaptchaIndex";
}

After this we will add a new controller file where we will display the captcha and perform validation of captcha.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_Demos.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string captchaValue)
        {
            if (captchaValue == Convert.ToString(Session["Captcha"]))
            {
                ViewBag.Message = "You have enter correct captcha.";
            }
            else
            {
                ViewBag.Message = "Pleae enter valid captcha.";
            }
            return View();
        }

    }
}

In above code I have added httpget and httpost method.  In this post method I have captured and validated weather the user have added correct captcha code or not.

Now we will create the view and add the below code.

@{
    ViewBag.Title = "Index";
}

<h2>Validate Captcha</h2><br />

@using (Html.BeginForm("Index", "Home"))
{
    @ViewBag.Message<br />
    <img src="/Captcha/CaptchaIndex/" /><br /><br />
    <input type="text" name="captchaValue" id="txtCaptcha" /><br /><br />
    <input type="submit" value="Submit" />
}

In above code I have bind the captchaindex view to the image control. This will be responsible for displaying the captcha code.

Now we have done run the application and check the output.



DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top