ASP. 5 Adventure (8): Using middleware, taghelper to implement CAPTCHA in MVC 6

Source: Internet
Author: User

(This article also published in my public number "dotnet daily Essence article", Welcome to the right QR code to pay attention to. )

Preface: Since ASP. NET 5 and MVC 6 are a new Web development platform for Microsoft, some of the existing code libraries have not been used directly, so I have implemented a simple code base--CAPTCHAMVC6.

CaptchaMVC6 I have open source to GitHub (and also provide reference for use examples), you can access the project page by reading the original.

CaptchaMVC6 Although some of the core algorithms are not fundamentally different from the existing code base, the entire package takes advantage of the new features of ASP. NET 5 and MVC 6:

    • Using the ASP. NET 5 middleware to render the captcha image, the usual way is to use HttpHandler, HttpModule, or directly with the controller of ASP. Related code See: CaptchaMiddleware.cs, CaptchaMiddlewareExtensions.cs
    • Based on the processing in the Taghelper simplified view. For example, to display a code image, the general practice is to contract an address, and in the IMG element directly to SRC set the service-side Verification code image rendering address. With Taghelper, you only need to add a custom attribute to the IMG element. In addition, through the Taghelper to control the verification code should not display (see below). Related code See: CaptchaTagHelper.cs
    • Based on the ASP. NET 5 Dependency injection mechanism, you can easily replace the code generation algorithm (I also provide a simple default algorithm, of course). Related code See: CaptchaServiceCollectionExtensions.cs, DefaultCodeGenerator.cs, DefaultGraphicGenerator.cs

In addition to these new features, CaptchaMVC6 has an additional two features:

    1. An algorithm based on Validationattribute to encapsulate the validity of a validation code on the server, that is, to validate the validity, you only need to attach a tag to the view model. Related code See: CaptchaAttribute.cs
    2. Implement a simple verification code is enabled by the control mechanism (through the session to record whether to open). Related code See: CaptchaControlTagHelper.cs

In the usage example, I demonstrated two usage scenarios:

    1. When registering, you must enter a verification code before you can complete the registration. Related codes See: Registerviewmodel class, register.cshtml in AccountViewModels.cs
    2. When logging in, you can not enter the verification code at the beginning, if the error two times, you will enter a verification code. Related codes See: Login method in Loginviewmodel class, login.cshtml, AccountController.cs in AccountViewModels.cs

It is important to note that because CaptchaMVC6 uses the System.Drawing assembly, it is temporarily unable to run on top of the Dnxcore50, that is, it cannot cross the platform. And this is still an original version, there are many areas that need to be perfected. In addition, I will also provide the NuGet package as soon as possible for everyone to use easily.

Project Address: Https://github.com/heavenwing/CaptchaMVC6

ASP. 5 Adventure (8): Using middleware, taghelper to implement CAPTCHA in MVC 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.