This is a migrated thread and some comments may be shown as answers.

Validation issues on postback with dynamic Captcha

8 Answers 643 Views
Captcha
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 07 Aug 2009, 11:05 PM
First of all thanks for a fantastic suite of tools Telerik, it goes from strength to strength.

I currently have a c# .Net v3.5 page with a placeholder, on page_init I add a dynamic captcha to the placeholder.
The problem I'm seeing is that the captcha is always returning invalid code on postback, I think due to the fact I am recreating the captcha on every postback.
Unfortunately I do not have the option to have the control in the aspx.

Has anybody had this issue? Any pointers to a good way of handling dynamic captchas would be appreciated.

Thanks for your time,
Pete

8 Answers, 1 is accepted

Sort by
0
Pero
Telerik team
answered on 12 Aug 2009, 03:40 PM
Hello Pete,

There shouldn't be any problems with the RadCaptcha when it is created dynamically in the Page_Init method. You can check my project (attached to the thread) and see that the RadCaptcha is validating the input correctly.

What I think might be causing the problem on your side is that you make AJAX calls before you try to validate the RadCaptcha. If a single AJAX call is fired RadCaptcha will always return the ErrorMessage that the input was invalid even if the correct code is entered. This is because, when an AJAX request is fired, the server-side page goes through its full page life-cycle. This means that a new RadCaptcha code will be generated, but because it's an AJAX update it would not be shown on the image (the old code is still shown because the RadCaptcha is not affected by the partial page update). So, the user sees the old code and therefore enters the old code. To overcome this issue just place the RadCaptcha in an UpdatePanel, so that it is updated on every single AJAX call.

If this is not your problem, please give us more information about your problem, and send us a simple running project where we can observe your behavior. Once we receive your project, we will do our best to provide a solution.

All the best,
Pero
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Anastasis
Top achievements
Rank 1
answered on 25 Aug 2009, 01:05 PM
Dear Admin,

I have a similar problem too.
I add a custom control inherited from user control dynamically in an aspx page in the onload event in each request.
This custom user control contains a captcha control added in design time. In every postback either the isvalid function of captcha or the isvalid function of page returns false even if the input code is correct.

I followed the instructions of yours (of moving the dynamic addition to OnInit method instead of OnLoad although I would rather avoiding it ) but nothing changed.

I am looking forward  your recommendations

Thanks in advance
0
Pero
Telerik team
answered on 28 Aug 2009, 07:33 AM
Hello Tasos,

When using dynamic RadCaptcha to validate user input, the control has to be created in the Page_Init method in order for the validation to be performed correctly. If the RadCaptcha is created in the Page_Load, the validation of the entered code occurs before the code itself has been loaded from the postback data. This means that the code will always be "null" and the RadCaptcha will always display the ErrorMessage telling the user that an invalid code has been entered. So, that is why the Page_Init method should be used instead.

For your convenience, I have created a sample project where I am adding the RadCaptcha to the UserControl at design time and loading the control itself in the Page_Init. Everything is working fine - the RadCaptcha validates the input correctly. To be sure that the right code is entered, there is no image noise and only numbers are rendered. You can find the project attached to this thread.

If you still experience problems, please modify my project (so that the problem is reproduced) and sent it back. We would be glad to help you.


Best wishes,
Pero
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Anastasis
Top achievements
Rank 1
answered on 31 Aug 2009, 07:22 AM
Dear Pero,

Thank you for the instant reply. You are absolutely right about the dynamic addition of user control in the OnInit event instead of OnLoad. By adding user control on the OnInit event the validator of the captcha control works fine! The problem is that moving the hole block of code from the OnLoad to the OnInit event may produce side effects to other controls being contained in the user control like captcha. I would like to avoid moving the code. Is there a way to overcome this? For example by using a certain technique or possibly by setting a value to a captcha property?

Thanks for the reply.
0
Pero
Telerik team
answered on 03 Sep 2009, 07:29 AM
Hello Tasos,

Well, you can call the RadCaptcha.Validate() method once again in the OnPreRender() method, after the control is loaded on the page and the RadCaptcha will validate the user input once more. So, load your UserControl in the Page_Load method and in the code behind of the UserControl.ascx (from the project that I sent you in the previous post) put the following source code:

UserControl.ascx.cs
protected override void OnPreRender(EventArgs e) 
    base.OnPreRender(e); 
       //Validate only on postback 
    if (IsPostBack) 
    { 
        RadCaptcha1.Validate(); 
    } 

This time the user input should be validated correctly.

Best wishes,
Pero
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Lynne
Top achievements
Rank 1
answered on 12 Jul 2010, 08:01 AM
I have the opposite problem - IsValid always returns True. I have reviewed the provided example without any luck.

My User Control is dynamically loaded from a SharePoint Web Part from the "protected override void OnInit(EventArgs e)" event. When I debug I can see my data from my other Text Boxes correctly rebind (from ViewState), however my "txtCaptcha" textbox is always empty.

Given the complexity of my solution it's very difficult to provide an example, however here's some stuff:

ASCX:

            <telerik:RadCaptcha ID="rcCaptcha" runat="server" ErrorMessage="Page not valid. The code you entered is not valid."
                ValidationGroup="vgAudio" ValidatedTextBoxID="txtCaptcha" Display="None" EnableAjaxSkinRendering="false">
                <CaptchaImage EnableCaptchaAudio="true" RenderImageOnly="true" ImageCssClass="rcCaptchaImage"
                    BackgroundColor="#609f0a" TextColor="White" BackgroundNoise="Low" />
            </telerik:RadCaptcha>    
            <asp:Label ID="rcLabel1" runat="server" AssociatedControlID="txtCaptcha" Text="Type the code from the image:"
                ForeColor="#005000"></asp:Label>
            <br />
            <asp:TextBox ID="txtCaptcha" runat="server" MaxLength="5" Width="170px"></asp:TextBox>           

Web Part Code:

protected override void OnInit(EventArgs e)
{
...
Control ctl = Page.LoadControl("/_layouts/XXX.ascx");
            ctl.ID = ControlIdXXXUC;
            XXXUC ucXXX = (XXXUC)ctl;
...

btnSubmit = new Button { Text = "Submit", CssClass = "ms-ButtonHeightWidth", Width = Unit.Parse("8.5em") };
btnSubmit.Click += btnSubmit_Click;

...
}

 private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Control ctl = FindControlRecursive(Page, ControlIdXXXUC);
                XXXUC ucXXX = (XXXUC)ctl;

                // CAPTCHA
                if (!ucXXX.IsCaptchaValid)
                {
                    ReportMessage("Please enter a valid Security Code");
                    return;
                }              
...

User Control Code

 public bool IsCaptchaValid
        {
            get
            {
                return rcCaptcha.IsValid;
            }
        }


As I said above, other controls return valid data but my "txtCaptcha" always seems to empty. I have also tried using the "Validate" method.
0
Lynne
Top achievements
Rank 1
answered on 12 Jul 2010, 08:24 AM
Ok I got it working by removing the following items in bold:

  <telerik:RadCaptcha ID="rcCaptcha" runat="server" ErrorMessage="Page not valid. The code you entered is not valid."
                ValidationGroup="vgAudio" ValidatedTextBoxID="txtCaptcha" Display="None" EnableAjaxSkinRendering="false">

Now I don't need to rely on checking the Captcha IsValid field, as the Page.IsValid check is sufficient. Still not sure why the txtCaptcha textbox is empty on post-back, but I'll have it leave it for now.
0
Pero
Telerik team
answered on 14 Jul 2010, 12:05 PM
Hi Stephen,

The RadCaptcha control clears the validated textbox. This behavior cannot be changed.

Sincerely yours,
Pero
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Captcha
Asked by
Pete
Top achievements
Rank 1
Answers by
Pero
Telerik team
Anastasis
Top achievements
Rank 1
Lynne
Top achievements
Rank 1
Share this question
or