Verification Code Captcha Large plug-in small
I do not know what year or months to complete oademo ah, in short, or step by step, this period of time to start, first do the landing. Some time ago studied the installation of MySQL and memcached services under CENTOS7, and tested the operation in C #, the result is OK.
Today to do a simple bootstarp based on the response to landing page (in fact, the template under the Web), whether it is a login or register it, will use the verification Code, formerly used GDI painted out, feel too ugly, Baidu's about. NET verification code Most also use this method, finally tried a bit of captcha, feel pretty good, so try to use.
Nugit console Install-package Captcha to the project on the line, download good there is a Readme.txt file, according to do a bit, very convenient
This is what the backstage code says.
[HttpPost] [Captchavalidation ("Cacode", "Examplecaptcha", "Incorrect CAPTCHA code!")] Public ActionResult exampleaction (bool captchavalid) { if (!captchavalid) { return Content ("no"); } Else { return Content ("OK"); } }
This is the front desk.
<form action= "/example/exampleaction" method= "POST" > @{mvccaptcha examplecaptcha = new Mvccaptcha (" Examplecaptcha "); Examplecaptcha.userinputid = "Cacode"; Examplecaptcha.imagestyle = BotDetect.ImageStyle.CaughtInTheNet2; @Html. Captcha (Examplecaptcha) @* @Html. TextBox ("Cacode") *@ <input type= "text" id= "Cacode" name= "Cacode" /> } <input type= "Submit" value= "GO"/> </form>
This is all done according to the readme, of course, there are registered routes
public class Routeconfig {public static void RegisterRoutes (RouteCollection routes) { routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. Ignoreroute ("{*botdetect}", new {Botdetect = @ "(. *) Botdetectcaptcha\.ashx"}); Routes. MapRoute ( name: "Default", URL: "{controller}/{action}/{id}", defaults:new {controller = "Home", action = "Index", id = urlparameter.optional} ); } }
The three steps on the line, and then test, the first to lose a wrong try, point go, Output No, very good, and then make a right, output OK quite can, then, accidentally lost or OK, then empty or OK???
Turn off the browser, regenerate and then open the test, even if the first time is wrong is output OK. There is also a problem point image will be transferred to https://captcha.org/captcha.html?asp.net this address.
Question one
This problem is supposed to be a good thing, the plug-in helps us do the cache verification code, and also set the expiration time, to print some properties to the page to see the situation
<form action= "/example/exampleaction" method= "POST" > @{mvccaptcha examplecaptcha = new Mvccaptcha ("Examplecaptcha"); Examplecaptcha.userinputid = "Cacode"; Examplecaptcha.imagestyle = BotDetect.ImageStyle.CaughtInTheNet2; @Html. Captcha (Examplecaptcha) @* @Html. TextBox ("Cacode") *@ <input type= "text" id= "Cacode" Name= "Ca Code "/> <ul> <li>helplinkurl: @exampleCaptcha .helplinkurl</li> <li>addinitscript: @exampleCaptcha .addinitscript</li> <li>additionalcssclasses: @exam Plecaptcha.additionalcssclasses</li> <li>additionalinlinecss: @exampleCaptcha. Additionalinlinec Ss</li> <li>addscriptinclude: @exampleCaptcha .addscriptinclude</li> <li >autoclearinput: @exampleCaptcha .autoclearinput</li> <li>autofocusinput: @examplEcaptcha.autofocusinput</li> <li>autoreloadtimeout: @exampleCaptcha. Autoreloadtimeout</li> ; <li>codelength: @exampleCaptcha .codelength</li> <li>codestyle: @exampleCaptcha. codestyle& Lt;/li> <li>codetimeout: @exampleCaptcha .codetimeout</li> <li>helplinke nabled: @exampleCaptcha .helplinkenabled</li> <li>helplinktext: @exampleCaptcha .helplinktext< /li> <li>iconsdivwidth: @exampleCaptcha .iconsdivwidth</li> <li>reloadic Onurl: @exampleCaptcha .reloadiconurl</li> <li>soundiconurl: @exampleCaptcha. soundiconurl</li > <li>imagestyle: @exampleCaptcha .imagestyle</li> <li>locale: @exampleC Aptcha. locale</li> <li> @exampleCaptcha .helplinkmode</li> <li&Gt @exampleCaptcha .webcaptcha.captchaimageurl</li> <li> @exampleCaptcha. WebCaptcha.CaptchaBase.Get Code ("Examplecaptcha", BotDetect.CodeGenerationPurpose.ImageGeneration). Captchacode</li> </ul>} <input type= "Submit" value= "GO"/> </fo Rm>
The first attribute should be the solution to problem two, but unfortunately, setting the values of Examplecaptcha.helplinkurl and Helplinktext is invalid, and the result is returned to the default value.
There is also codetimeout is also invalid settings. In the Webconfig
<sessionstate mode= "InProc" cookieless= "AutoDetect" timeout= "sessionidmanagertype=" BotDetect.Web.CustomSessionIdManager, Botdetect "/>
Setting the value of timeout less than 20 is also invalid, there may be a minimum limit, here I have not tried more than 20 of the value, Halo!
Everything else is okay.
The last attribute is that I'm trying to find out the current CAPTCHA, and it's still unfortunate, not that I can't get it yet. One of the reasons I think it helps me do is to cache the verification code in the session, but I don't want to use the session, if it's just saved in a cookie, that iers good, and I don't need to get a verification code.
Look at the request to click the Refresh button
You can see that there are three get requests, the first two are the buttons that request the refresh and the sound, and the next one is the request picture. Mainly look at the back one, there appears to be a request for cookies, and there are two keys. Nevertheless, I am not sure if I have used the session cache, and I hope that Daniel, who is passing by , will give me guidance.
Take a look at the parameters, there are four parameters, each request only the value of D is changed, this is with our previous verification code refresh is a meaning. This request address is exactly the attribute value of EXAMPLECAPTCHA.WEBCAPTCHA.CAPTCHAIMAGEURL.
Let's see what the HTML of the Refresh button will ultimately look like, such as the onclick of the A tag (the red box), although it doesn't see the exact method, but you know the end result of the method is to complete the above request. Can understand this, the others are understandable.
Second, I don't like to write too much C # code on the front end. Look at the back-end code
[Captchavalidation ("Cacode", "Examplecaptcha", "Incorrect CAPTCHA code!")]
Public ActionResult exampleaction (bool captchavalid) ...
For action parameter Captchavalid I am puzzled, when I change the name of the parameter will be an error, think should be captchavalidationattribute in the bar, think with this attribute limit too much, not flexible. Finally I removed the attribute, using the static method of the Mvccaptcha class Mvccaptcha.validate (string,string,string), this overload requires three parameters one is the ID of the CAPTCHA, one is the user input value, The other is Validtinginstanceid.
For the last parameter I began to touch the north, behind in the HTML source to find a hidden input, just feel like it, because it has the Name property, with the feeling of the test, did not want to be good
Then change the background to try, in fact, the value of the last parameter is Examplecaptcha.currentinstanceid this property value (sometimes I think I am a little silly cute), so that the problem solved some of the problems, you can not write so many C # code at the front end, and each landing page to enter the right verification code to OK.
Public ActionResult exampleaction (string cacode, String Bdc_vcid_examplecaptcha) { string instaneid = Customcaptcha.currentinstanceid; BOOL captchavalid= mvccaptcha.validate ("Examplecaptcha", Cacode,bdc_vcid_examplecaptcha); if (captchavalid) return Content ("OK"); Return Content ("no"); }
question two
Solve the problem I used the method is very rough, I later redo an MVC project, did a full landing page, the refresh and voice button I am not good layout, directly to hide, since it is rude to change the value of the property can not be changed to verify the image of the click Link, I can not say let users point pictures into a foreign site, Then I can only rough through jquery to solve the problem, I think the solution is to the previous about the refresh button of the A-tag onclick to the image of the onclick, simple rough.
Although the two problems have not been solved perfectly, but feel that they are still working hard. So finally, put all the changes in the code.
Final effect
Front
View Code
The backend specifically writes a class that has a static property that specifies the instance ID of the current CAPTCHA, a static method for customizing the CAPTCHA
Using Botdetect.web.mvc;namespace oademo.mvcapp{public class Customcaptcha {//<summary>// Instance ID of the current CAPTCHA///</summary> public static string Currentinstanceid {get {return currentinstanceid;}} private static string Currentinstanceid; public static Mvccaptcha Getcaptcha (String captchaid) {Mvccaptcha Examplecaptcha = new Mvccaptcha (CAPTC HAID); Examplecaptcha.imagestyle = BotDetect.ImageStyle.CaughtInTheNet2; examplecaptcha.reloadenabled = false; examplecaptcha.soundenabled = false; Currentinstanceid = Examplecaptcha.currentinstanceid; return Examplecaptcha; }///<summary>///Get custom CAPTCHA styles///</summary>//<param name= "Captchaid" >& lt;/param>//<param name= "width" > Specify the width of the verification code picture </param>//<param name= "Height" > Specify the CAPTCHA image Height </param>//<param name= "codElength "> Specify the number of verification codes </param>//<returns></returns> public static Mvccaptcha Getcaptcha (s Tring captchaid,int width,int height,int codelength) {Mvccaptcha Examplecaptcha = new Mvccaptcha (captch AID); Examplecaptcha.imagestyle = botdetect.imagestyle.caughtinthenet2;//My personal favorite style examplecaptcha.reloadenabled = Fals e;//Remove the refreshed button examplecaptcha.soundenabled = false;//Remove the sound play button examplecaptcha.codelength = codelength;// Specifies the length of the verification code examplecaptcha.imagesize = new System.Drawing.Size (width,height);//Specifies the size of the picture currentinstancei D = examplecaptcha.currentinstanceid;//ID of the current instance return Examplecaptcha; } }}
Back-end controller code
Controller Code
Well, the back-end controller code should be that way.
Using System.web.mvc;namespace oademo.mvcapp.controllers{public class Logincontroller:controller { // Get:login public ActionResult Index () { return View (); } Public ActionResult Account (string Captchacode, string Username, string password) { string instaneid = Customcaptcha.currentinstanceid; BOOL captchavalid= mvccaptcha.validate ("Examplecaptcha", Captchacode,instaneid); if (captchavalid) return Content ("OK"); Return Content ("No");}}}
Now wrote here, of course, after the verification has not written, but also only in the test captcha stage, so want to say to the source of comrade, do not laborious, because here forget the password and registration are not written. This article is only written in my current study of Captcha, perhaps in a number of Mvccaptcha class events, is really effective solution to the problem, but I do not have English level, plus now too late, eyes and head can not stand, temporarily, the incident so much, understand, Webconfig configuration can be customized
public static event eventhandler<generatedcaptchacodeeventargs> Generatedcaptchacode; public static event eventhandler<generatedcaptchaimageeventargs> Generatedcaptchaimage; public static event eventhandler<generatedcaptchasoundeventargs> Generatedcaptchasound; public static event eventhandler<generatingcaptchacodeeventargs> Generatingcaptchacode; public static event eventhandler<generatingcaptchaimageeventargs> Generatingcaptchaimage; public static event eventhandler<generatingcaptchasoundeventargs> Generatingcaptchasound; public static event eventhandler<initializedwebcaptchaeventargs> Initializedwebcaptcha; public static event eventhandler<validateduserinputeventargs> Validateduserinput; public static event eventhandler<validatinguserinputeventargs> Validatinguserinput;
Verification Code Captcha plug-in