<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>recaptcha - Mr. Roa</title>
        <description>The name is Roa. Mr. Roa</description>
        <link>/Tags/recaptcha/RSS</link>
        <language>en</language>
        <image>
            <url>http://mrroa.com/Content/icons/mushroom.png</url>
            <title>Mr. Roa</title>
            <link>/Tags/recaptcha/RSS</link>
            <width>64</width>
            <height>64</height>
        </image>
        <item>
            <dc:creator>Admin</dc:creator>
            <title>Oxite: Customizing the comments area</title>
            <description>&lt;p&gt;Continuing my Oxite series of blog posts I'll address the comments area. For my taste oxite is missing a couple of things on the comments listing and comments posting department. It doesn't have a way to ensure that the comment being posted is not being generated by a bot. I've already been a victim of this and to be honest I'm not a fan of SPAM, so let's stop that!&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://s.mrroa.com/oxite-spam.png&quot; alt=&quot;&quot; width=&quot;727&quot; height=&quot;314&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Oxite also doesn't have a special style for comments posted by the site admin. In this blog post we are going to address this issues by implementing a css class that helps the reader identify a site administrator comment from a blog comment's list and a &lt;a title=&quot;Captcha&quot; href=&quot;http://en.wikipedia.org/wiki/CAPTCHA&quot; target=&quot;_blank&quot;&gt;captcha&lt;/a&gt; validation for the comments input.&lt;/p&gt;
&lt;p&gt;For these tasks I'll be using css and &lt;a title=&quot;Re Captcha&quot; href=&quot;http://recaptcha.net/&quot; target=&quot;_blank&quot;&gt;reCAPTCHA&lt;/a&gt;, which is owned by google and it's widely used among popular sites as Facebook, The New York Times, StackOverflow, TicketMaster, etc. It's worth pointing out that reCaptcha was initially conceived in Carnegie Mellon University, where the term &quot;CAPTCHA&quot; was initially coined. The term contrives from the acronym&amp;nbsp; &quot;&lt;strong&gt;C&lt;/strong&gt;ompletely &lt;strong&gt;A&lt;/strong&gt;utomated &lt;strong&gt;P&lt;/strong&gt;ublic &lt;strong&gt;T&lt;/strong&gt;uring test to tell &lt;strong&gt;C&lt;/strong&gt;omputers and &lt;strong&gt;H&lt;/strong&gt;umans &lt;strong&gt;A&lt;/strong&gt;part.&quot;&lt;/p&gt;
&lt;p&gt;So putting the theory to a side, let's start getting things done!&lt;/p&gt;
&lt;h3&gt;Styling the Admin comments&lt;/h3&gt;
&lt;p&gt;When the admin posts a comment Oxite automatically adds an &quot;Admin&quot; class to the li element from the comments list. This means that styling the comment won't be very hard since the only thing to do is add a custom style for this class. I chose a beige tone as the background of the Admin comments and added the following to the site stylesheet (site.css)&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;ul.comments.medium li.Admin
{
    background: #FFFFC0;
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And tha
&lt;script src=&quot;../../../Skins/Default/Scripts/tiny_mce/themes/advanced/langs/en.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;../../../Skins/Default/Scripts/tiny_mce/themes/advanced/langs/en.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
t's it, from now on my comments will have a beige like background. That was easy wasn't it? Good let's keep moving!&lt;/p&gt;
&lt;h3&gt;Implementing reCAPTCHA&lt;/h3&gt;
&lt;p&gt;Moving on to the CAPTCHA part, we have to do a couple things before starting. First register at&amp;nbsp;&lt;a title=&quot;Re Captcha&quot; href=&quot;http://recaptcha.net/&quot; target=&quot;_blank&quot;&gt;reCAPTCHA&lt;/a&gt;, download the .NET dll and reference it from Oxite.Mvc project. After that the first thing to do is create an Action Filter Attribute. There's already a directory containing existing Filters for Oxite on the Oxite.Mvc project so let's place ours there.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;namespace Oxite.Mvc.ActionFilters
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;public class CaptchaAttribute : ActionFilterAttribute
&amp;nbsp;&amp;nbsp; &amp;nbsp;{
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private const string ChallengeFieldKey = &quot;recaptcha_challenge_field&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private const string ResponseFieldKey = &quot;recaptcha_response_field&quot;;

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public override void OnActionExecuting(ActionExecutingContext filterContext)
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{
            //Getting the user parameter from the current context
            var user = (Model.User)filterContext.ActionParameters[&quot;currentUser&quot;];
            if (user != null &amp;amp;&amp;amp; user.Name == &quot;Admin&quot;)
            {
                filterContext.ActionParameters[&quot;captchaValid&quot;] = true;
                return;
            }
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var captchaChallengeValue = filterContext.HttpContext.Request.Form[ChallengeFieldKey];
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var captchaResponseValue = filterContext.HttpContext.Request.Form[ResponseFieldKey];

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var captchaValidtor = new Recaptcha.RecaptchaValidator
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PrivateKey = &quot;YOUR PRIVATE KEY GOES HERE&quot;,
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RemoteIP = filterContext.HttpContext.Request.UserHostAddress,
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Challenge = captchaChallengeValue,
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Response = captchaResponseValue
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;};

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var recaptchaResponse = captchaValidtor.Validate();
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;filterContext.ActionParameters[&quot;captchaValid&quot;] = recaptchaResponse.IsValid;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;base.OnActionExecuting(filterContext);

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In order to render the reCAPTCHA widget we are going to add a new helper to the Html helper Extensions class. The helper is going to ease the implementation by encapsulating everything to a method call. Our new extension method should look like this:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;#region ReCaptcha
public static string RenderCaptcha(this HtmlHelper helper)
{
&amp;nbsp; &amp;nbsp; &amp;nbsp; var captchaControl = new Recaptcha.RecaptchaControl
 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{
 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ID = &quot;recaptcha&quot;,
 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Theme = &quot;clean&quot;,
 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PublicKey = &quot;YOUR PUBLIC KEY GOES HERE&quot;,
 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;PrivateKey = &quot;YOUR PRIVATE &lt;script src=&quot;../../../Skins/Default/Scripts/tiny_mce/themes/advanced/langs/en.js&quot; type=&quot;text/javascript&quot;&gt;&lt;!--mce:2--&gt;&lt;/script&gt;KEY GOES HERE&quot;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;};

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var htmlWriter = new HtmlTextWriter(new StringWriter());
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;captchaControl.RenderControl(htmlWriter);
 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return htmlWriter.InnerWriter.ToString();
}
#endregion&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now we are ready for the implementation. From here we just need to add our Action Filter Attribute on the Controller method that creates the post comment. The Controller is located on the same project as the Action Filers, Oxite.Mvc. The comments are created on the PostController. There we need to add our Action Filter Attribute to the AddComment method. Also we need to change the method signature by adding a new overload by adding a new variable to contain our reCAPTCHA validation result.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;[CaptchaValidator]
[ActionName(&quot;Item&quot;), AcceptVerbs(HttpVerbs.Post)]
public virtual object AddComment(Area areaInput, PostBase postBaseInput, Comment commentInput, UserBase userBaseInput,&amp;nbsp;
                                   UserBase currentUser,&amp;nbsp;bool? remember, bool? subscribe, bool? captchaValid)&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Now the success of the CAPTCHA validation is going to be contained on the added boolean value. Notice that the type is marked as nullable, this is because the method is initialized with null values on the OxiteApplication class.&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;itemActionCriteria.AddMethod(p =&amp;gt; p.AddComment(null, null, null, null, null, null, null, null));&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Great, so now we have almost everything in place. I used a quick &amp;amp; dirty approach in order to raise the form validation when a the introduced string does not match the one displayed by the reCAPTCHA widget. In case the captchaValid variable is false I just set the comment on the&amp;nbsp;&lt;span style=&quot;white-space: pre;&quot;&gt;commentInput object to String.Empty.&lt;span style=&quot;white-space: normal;&quot;&gt;&amp;nbsp;This will set the validationState object IsValid flag to false, which is Oxite's form validation mechanism.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;&lt;span style=&quot;white-space: normal;&quot;&gt;Now the only thing left to do is go to the&amp;nbsp;CommentFromAnonymous.ascx user control. This is what renders the actual comment form on the post. Here we are going to add our Html helper extension method in order to render the reCAPTCHA widget and finally add some validation to the post comment form. I decided to place my widget under the actual comment text area. The call to the extension method looks like this:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;lt;div class=&quot;recaptcha&quot;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;%= Html.RenderCaptcha() %&amp;gt;&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;/pre&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;p&gt;In this blog post I added an extra, yet important, feature to Oxite: CAPTCHA validation. Once again, I was able to extend the current core with ease and added reCAPTCHA's widget. I'm so glad I did this, since I wont have to deal with spam comments anymore :). Stay tuned for some other Oxite extensions and more praising of the MVC paradigm!&lt;/p&gt;</description>
            <link>http://mrroa.com/Blog/Oxite-Customizing-the-comments-area</link>
            <guid isPermaLink="true">http://mrroa.com/Blog/Oxite-Customizing-the-comments-area</guid>
            <pubDate>Tue, 27 Oct 2009 05:19:00 GMT</pubDate>
            <category>recaptcha</category>
            <category>Oxite</category>
        </item>
    </channel>
</rss>
