The Research
Knowing that the captcha is an available option for the registration page I started by looking in the /Admin/Security/Register.ascx page to see if I could find the code implemented there. This ended up turning up a few loose ends as the User.ascx control was used to provide a good majority of the code for the page. So the next step was to look at the /Admin/Users/User.ascx file, in there I found the key that I needed, the Captcha is a compiled control that is part of the DotNetNuke.UI.WebControls namespace in the DotNetNuke assembly. Now, I have an example that I could work with to put the Captcha in my page.The Implementation
Now that I found the information it was very easy to implement the Captcha in my project. There are a total of 3 pieces of code that you must insert into your project to successfully use the Captcha control. Each of these items will be provided and detailed below.Register the Assembly
The first step is to add the proper register declaration to your user control. This statement is what allows you to later declare and use the Captcha control in your page. You can simply copy and paste this code to the top portion of your .ascx file.%@ Register TagPrefix="dnn" Assembly="DotNetNuke" Namespace="DotNetNuke.UI.WebControls"%>
Declare the Control
The next step is to actually place an instance of the control in your module. Below is an example of a default configuration of the control, many other options exist for you to style the module to meet your needs, you can also implement a ResourceKey to allow localization of the error message.dnn:CaptchaControl ID="ctlCaptcha" CaptchaHeight="40" CaptchaWidth="150"
ErrorStyle-CssClass="NormalRed" cssclass="Normal" runat="server"
ErrorMessage="The typed code must match the image, please try again"/>
As you can see this is a pretty simple step to complete, I have set the height and width of the captcha image to ensure that it fits nicely inside my area on the control, I also was sure to set the ErrorStyle and CssClass attributes to ensure that everything displays nicely with my skin. You have other options that you can configure here as well, including the number of characters and other items of that nature.ErrorStyle-CssClass="NormalRed" cssclass="Normal" runat="server"
ErrorMessage="The typed code must match the image, please try again"/>
Validation of Captcha
The final step is to implement code in the backend that validates the Captcha to ensure that the user supplied information was correct. Unlike other validation controls the Captcha does NOT validate prior to postback so this step is mandatory to ensure that the users supplied the Captcha. Below is some sample C# code to validate the Captcha, just a simple if statement.if (ctlCaptcha.IsValid)
{
//Do your processing!
}
{
//Do your processing!
}
No comments:
Post a Comment