Posted by Anuraj on Wednesday, September 2, 2015 Reading time :1 minute
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.
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.
You can use the Tag Helper like this.
To validate, I am using the Request.Form collection to get the hidden field value.
You can find the full source code of CaptchaTagHelper on GitHub
Happy Programming :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.