+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / issue with Captcha image

issue with Captcha image

12 posts, 0 answered
  1. Biren
    Biren avatar
    23 posts
    Registered:
    02 Nov 2010
    08 Mar 2011
    Link to this post
    Hi,
    We are using <sfWeb:SpamProtector> control on blog detail page to allow visitors to add comments on the blog post. But most of the time page loads with empty captcha image [gray box, refer attached file]. After refreshing page multiple time proper captcha image appears. But even posting comment action fails with error "Invalid captcha" most of the time. This is really hindering user experience. Captcha image on sitefinity login page behaves the same. How i can solve the issue? Is there any other sitefinity control which is more stable ?

    Note* We are running multiple instance of the site with loadbalancer.

    Thanks,
    Biren

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    08 Mar 2011
    Link to this post
    Hello Biren,

    • Web Farm - The application runs on more than one web server at the same time.
    • Web Garden - The application runs on a single server, but the server load is divided among many worker processes (more than one process are running the same application).

    RadCaptcha stores the CaptchaImage in the Cache object, by default. Usually, every server (or every worker process) has an independent Cache, which means that, when the page request is not handled by the same web server (worker process), the CaptchaImage will be null and a gray image will be shown.
    To avoid this issue, you should store the CaptchaImage in the Session, as it was suggested in one of the previous posts and configure your server environment to use out of process SessionState ( the Session object is shared among different processes and servers).

    • Set the ImageStorageLocation property to Session;
    • Configure the httpHandler in the following way:
    You must register the handlers below when you use session.
    Copy Code
    <configuration>
         
    <system.web>
         
    <httpHandlers>
         
          <add path="Telerik.Web.UI.WebResource.axd"type="Telerik.Web.UI.WebResourceSession"verb="*"validate="false"/>
         
    </httpHandlers>
         
    </system.web>
         
           
         
    <system.webServer>
         
    <handlers>
         
          <add name="Telerik_Web_UI_WebResource_axd"verb="*"preCondition="integratedMode"path="Telerik.Web.UI.WebResource.axd"type="Telerik.Web.UI.WebResourceSession"/>
         
    </handlers>
         
    </system.webServer>
         
    </configuration>



    Greetings,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. Biren
    Biren avatar
    23 posts
    Registered:
    02 Nov 2010
    08 Mar 2011
    Link to this post
    Thanks for the reply Ivan. Quick question how i can integrate RadCaptcha control with SpamProtector control ? SpamProtector control doesn't have property called ImageStorageLocation . And if i remove the control SpamProtector and add control RadCaptcha then captcha validation doesn't happen when i try to post comment. Please advice.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    09 Mar 2011
    Link to this post
    Hi Biren,

    You can use RadCaptch instead of SpamProtector, but  you should override the behavior of the BlogCommentsList control, so that you can check whether the request is valid or not. Another option would be working on a template level and canceling the submit request of the form, but you need to use recursion to access all controls from the template.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  5. Biren
    Biren avatar
    23 posts
    Registered:
    02 Nov 2010
    10 Mar 2011
    Link to this post
    Hi Ivan,
    As per your suggestion i did following changes.

    1) created new user control myBlogCommentsList, derived it from BlogCommentsList

    public partial class myBlogCommentsList : BlogCommentsList
        {
            protected override void CreateChildControls()
            {
                if (this.Page == null)
                    this.Page = new Page();
     
                base.CreateChildControls();
     
                this.SubmitButton.Click += new EventHandler(SubmitButton_Click);
            }
     
            void SubmitButton_Click(object sender, EventArgs e)
            {
                RadCaptcha captcha = this.Container.FindControl("captcha") as RadCaptcha;
                 
                // check validity of the control
                if (captcha.IsValid)
                { }
     
                // compare text with captcha text
                TextBox captchaText = captcha.FindControl("CaptchaTextBox") as TextBox;
                if (captcha.CaptchaImage.Text == captchaText.Text)
                { }
            }
        }

    2) replaced spamProtector control with RadCaptcha
    <ctl:myBlogCommentsList ID="commentsList" runat="server" CssClass="sf_commentsList"
                    ValidationGroup="commentInfo">
                <%--<sfWebblog:BlogCommentsList ID="commentsList" runat="server" CssClass="sf_commentsList" ValidationGroup="commentInfo">--%>
                    <LayoutTemplate>
                        <asp:PlaceHolder ID="moderationHolder" runat="server" Visible="false">
                            <p class="sf_commentMessage" style="color: Green">
                                <asp:Literal ID="Literal2" runat="server" Text="<%$Resources:CommentMessageModeration%>"></asp:Literal>
                            </p>
                        </asp:PlaceHolder>
                        <fieldset class="postNewComment">
                            <span class="leaveCommnethHeader">
                                <asp:Literal ID="Literal3" runat="server" Text="<%$Resources:LeaveComment%>" /></span>
                            <ol>
                                <li>
                                    <asp:Label ID="Label2" runat="Server" Text="Name" AssociatedControlID="authorTxt"
                                        CssClass="commentOptionHeader"></asp:Label>
                                    <asp:TextBox ID="authorTxt" runat="server" size="30"></asp:TextBox>
                                    <asp:RequiredFieldValidator runat="server" ID="authorVal" ControlToValidate="authorTxt"
                                        ErrorMessage="Name is required" Display="dynamic" CssClass="sf_commentsValidator"></asp:RequiredFieldValidator>
                                </li>
                                <li class="hideElement">
                                    <asp:Label ID="Label3" runat="Server" Text="Email" AssociatedControlID="emailTxt"></asp:Label>
                                    <asp:TextBox ID="emailTxt" runat="server" size="30"></asp:TextBox>
                                    <asp:RegularExpressionValidator runat="server" ID="emailVal" ControlToValidate="emailTxt"
                                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="InvalidEmail"
                                        Display="dynamic" CssClass="sf_commentsValidator"></asp:RegularExpressionValidator>
                                </li>
                                <li class="hideElement">
                                    <asp:Label ID="Label4" runat="Server" Text="WebSite" AssociatedControlID="webSiteTxt"></asp:Label>
                                    <asp:TextBox ID="webSiteTxt" runat="server" size="30"></asp:TextBox>
                                </li>
                                <li class="texarea-wrapper">
                                    <div>
                                        <div class="commentOptionHeaderDiv">
                                            <span style="float: left" class="commentOptionHeader">
                                                <asp:Literal ID="Literal4" runat="server" Text="<%$Resources:Comment%>" /></span><br />
                                        </div>
                                        <telerik:RadEditor runat="server" ID="commentTxt" ToolsFile="~/Sitefinity/ControlTemplates/Blogs/ToolsFile.xml"
                                            EnableEmbeddedSkins="True" Skin="Default" Width="100%" EditModes="Design" Height="200px" BorderWidth="0" BorderStyle="None" >
                                        </telerik:RadEditor>
                                    </div>
                                    <asp:RequiredFieldValidator runat="server" ID="contentVal" ControlToValidate="commentTxt"
                                        ErrorMessage="CommentEmpty" Display="dynamic" ValidationGroup="commentInfo" CssClass="sf_commentsValidator">
                                        <asp:Literal ID="Literal6" runat="server" Text="<%$Resources:MessageTextRequired%>" />
                                    </asp:RequiredFieldValidator>
                                </li>
                                <li>
                                    <telerik:RadCaptcha runat="server" ImageStorageLocation="Session" ValidationGroup="sf_commentsList" ID="captcha"></telerik:RadCaptcha>
                                    <asp:Button ID="submitBtn" runat="server" Text="Post" CssClass="sf_commentSubmit postCommentbtn" />
                                </li>
                            </ol>
                        </fieldset>
                        <asp:Repeater ID="commentsRepeater" runat="Server">
                            <HeaderTemplate>
                                <asp:PlaceHolder runat="server" ID="headerHolder"></asp:PlaceHolder>
                                <div id="commentsCountdiv" style="display: none">
                                    <asp:Literal ID="commentsCount" runat="server"></asp:Literal>
                                </div>
                                <ul class="postComment">
                            </HeaderTemplate>
                            <ItemTemplate>
                                <!-- comment goes here -->
                                <sfWeb:CommentItem ID="anonCommentContainer" runat="server">
                                    <Template>
                                        <p class="commentAuthor">
                                            <b>
                                                <asp:HyperLink ID="webSiteLink" runat="server" Visible="false" rel="nofollow"></asp:HyperLink>
                                                <asp:Literal runat="Server" ID="authorName" Visible="false"></asp:Literal>
                                            </b>
                                            <%--<abbr class="sf_commentDate" id="commentDate" runat="server" > </abbr>--%>
                                            <telerik:DateTimeLiteral ID="commentDateLiteral" runat="server" DateFormatString="MM/dd/yyyy a\t h:mm tt" />
                                        </p>
                                        <asp:Label ID="text" runat="server" CssClass="postCommentLiDivSpan"></asp:Label>
                                    </Template>
                                </sfWeb:CommentItem>
                                <sfWeb:CommentItem ID="authorCommentContainer" runat="server">
                                    <Template>
                                        <p class="commentAuthor">
                                            <b>
                                                <asp:HyperLink ID="webSiteLink" runat="server" Visible="false" rel="nofollow"></asp:HyperLink>
                                                <asp:Literal runat="Server" ID="authorName" Visible="false"></asp:Literal>
                                            </b>
                                            <%--<abbr class="sf_commentDate" id="commentDate" runat="server"> </abbr>--%>
                                            <telerik:DateTimeLiteral ID="commentDateLiteral" runat="server" DateFormatString="MM/dd/yyyy a\t h:mm tt" />
                                        </p>
                                        <asp:Label ID="text" runat="server" CssClass="postCommentLiDivSpan"></asp:Label>
                                    </Template>
                                </sfWeb:CommentItem>
                            </ItemTemplate>
                            <FooterTemplate>
                                </ul>
                            </FooterTemplate>
                        </asp:Repeater>
                    </LayoutTemplate>
                <%--</sfWebblog:BlogCommentsList>--%>
    </ctl:myBlogCommentsList>

    3) changed web.config to store session on SQL server

    Everything works fine except validating RadCaptcha control. On click event of "POST" button i tried doing couple of check
    a) i check if RedCaptcha is valid using isValid property. Value of this property is always true even if i enter incorrect text.
    b) compare captcha.CaptchaImage.Text and user entered text. User entered text is always empty and CaptchaImage is reloaded with new image.

    None of this check works. Am i doing something wrong here ?

    -Biren
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    10 Mar 2011
    Link to this post
    Hi Biren,

    The RadCaptcha validates on postback. It depends at what stage of the ASP.NET page lifecycle you check the IsValid property of the RadCaptcha, because the validation might not have been performed yet. You could try to call RadCaptcha.Validate(), and then Page.Validate().

    You can try to set some session value that indicates whether the RadCaptcha is valid which should explicitly validate the control. Note that if you want to stop the submit you need to preform Response.Redirect ( this is the easiest way).

    public void Page_Load(object sender, EventArgs e)
       {
           saveButton.Click += new EventHandler(saveButton_Click);
           if (!valid)
           {
               RadCaptcha1.CaptchaTextBoxLabel = "Please type a valid key";
           }
       }
      
       void saveButton_Click(object sender, EventArgs e)
       {
           if (!Page.IsValid)
           {
               valid = false;
             
           }
           else
           {
               valid = true;
           }
       }
      
       public bool valid
       {
           get
           {
               object o = Session["valid"];
               if (o == null)
               {
                   return true;
               }
               return (bool)o;
           }
           set
           {
               this.Session["valid"] = value;
           }
       }



    Greetings,
    Ivan Dimitrov
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  7. Biren
    Biren avatar
    23 posts
    Registered:
    02 Nov 2010
    22 Mar 2011
    Link to this post
    On postback radcaptach control gets reloaded. Even blog post detail is also getting reloaded. I am not following your suggestions. Can you please provide more detail on integrating Radcaptch with blogcommentsList control. Thanks !!
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    22 Mar 2011
    Link to this post
    Hello Biren,

    Could you tell me what exactly is not clear? Here it does not matter that the RadCaptch is reloaded. The important part is the input and whether the control is validated.

    All the best,
    Ivan Dimitrov
    the Telerik team
  9. Biren
    Biren avatar
    23 posts
    Registered:
    02 Nov 2010
    22 Mar 2011
    Link to this post
    Hi Ivan,
    I agree that it is not important whether RadCaptcha gets reloaded or not. Important part is where I validate the RadCaptach. That is the part which is not clear to me. I will brief you on what i have done so far.
    1) i have custom template[named DisplaySingleItem] to display blog post details. User is allowed to comment on it. 
    2) created new user control myBlogCommentsList, derived it from BlogCommentsList. [code on 9th March reply]. In CreateChildControls method i subscribe to click event of SubmitButton. In the event handler i try to check whether RabCaptcha is valid or not.
    3) On custom template[DisplaySingleItem] i replaced spamProtector control with RadCaptcha

    Now when i click POST button on post detail page, Control goes to method CreateChildControls which reloads the control and then control goes to event handler of click event. So at this point checking the RadCaptcha doesn't matter.

    You also suggested that i should validate RadCaptcha on PostBack and store the result to session. Not sure where/when this validation should happen. I tried to put this check at few diff places like page load or on init of master template, on init of myBlogCommentsList, no luck so far.

    I want to know what you would do to replace spamProtector with RadCaptcha and integrate the validation.

    Thanks,
    Biren
  10. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    10 Jun 2011
    Link to this post
    Hi Ivan,

    I'm using the RadCaptcha control that comes along with Sitefinity 3.7 SP3 and it works perfectly fine when I'm setting the ImageLocation property to Cache. But somehow it fails when the property value is changed to Session. I've done all the changes required to the web.config to refer the correct httphandler.

    Please let me know where am I going wrong.

    Thanks,
    Saumitra
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    14 Jun 2011
    Link to this post
    Hi Saumitra,

    Could you send me the control declaration and the web.config changes you made. I was not able to replicate this issue locally.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  12. Saumitra
    Saumitra avatar
    127 posts
    Registered:
    10 Mar 2009
    15 Jun 2011
    Link to this post
    Hi Ivan,

    Thanks for the support, but my issue got resolved. It was a small mistake from my side in the web.config. I had made the changes related to the httphandler in the <system.webserver> section only, whereas the IIS version running on the concerned machine was 6.0. I made the relevant changes in the <system.web> section and that did the trick.

    Thanks,
    Saumitra
Register for webinar
12 posts, 0 answered