Click here to Skip to main content
15,891,607 members
Articles / All Topics

Implementing basic Captcha in ASP.NET 5 MVC 6

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
19 Sep 2015MIT 9.9K   3   4
Long back I wrote some posts about implementing captcha in ASP.NET MVC. This post is about implementing captcha in ASP.NET5 MVC 6. How it works – while loading the page, captcha tag helper displays an image, and set a hidden field in the page with an encrypted value.

Long back I wrote some posts about implementing captcha in ASP.NET MVC. This post is about implementing captcha in ASP.NET5 MVC 6.

How it works – while loading the page, captcha tag helper displays an image, and set a hidden field in the page with an encrypted value. Once users submits the page, this encrypted value validates against the captcha user input. If it is same the entry is accepted otherwise it show the error. For this post I am using very basic Base64 encryption.

ASP.NET5 doesn’t have any nuget packages which supports drawing, so you may need to use the .NET System.Drawing namespace. First you need to include the System.Drawing namespace in the project.json framework assemblies list.

"frameworks": {
  "dnx451": {
    "frameworkAssemblies": {
        "System.ServiceModel": "4.0.0.0",
        "System.Drawing": "4.0.0.0"
    }
  }
}

Instead of returning the image as Image content type, I am using HTML5 image data feature, converting the image to a Base64 string, and setting it to the src attribute of the image.

bitmap.Save(memoryStream, ImageFormat.Jpeg);
byte[] imageBytes = memoryStream.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
output.Attributes["src"] = "data:image/png;base64," + base64String;

You can use the Tag Helper like this.

<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10" />
    <div>
        <img asp-captcha="true" class="img-thumbnail"/>            
    </div>
    </div>
</div>
Captcha in ASP.NET 5

Captcha in ASP.NET 5

To validate, I am using the Request.Form collection to get the hidden field value.

var captchaImage = Context.Request.Form["__captcha_image"];
var encryptedString = 
Convert.ToBase64String(UTF32Encoding.Unicode.GetBytes(user.Captcha));
if (captchaImage != encryptedString)
{
    ModelState.AddModelError("", "Captcha is not matching.");
    return View("SignUp");
}

You can find the full source code of CaptchaTagHelper on GitHub

Happy Programming :)

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Technical Lead
India India
Working as Tech. Lead

My blog : dotnetthoughts.net.
You can follow me in twitter : @anuraj

Comments and Discussions

 
Suggestion[My vote of 1] Base64 is an encoding NOT an encryption ! Pin
ObiWan_MCC22-Sep-15 21:07
ObiWan_MCC22-Sep-15 21:07 
GeneralRe: [My vote of 1] Base64 is an encoding NOT an encryption ! Pin
Anuraj Parameswaran24-Sep-15 18:03
Anuraj Parameswaran24-Sep-15 18:03 
GeneralRe: [My vote of 1] Base64 is an encoding NOT an encryption ! Pin
ObiWan_MCC24-Sep-15 20:51
ObiWan_MCC24-Sep-15 20:51 
GeneralMy vote of 5 Pin
JoshYates198021-Sep-15 8:05
professionalJoshYates198021-Sep-15 8:05 
simple and effective.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.