Stopping Spam posts in c# with akismet.net

Posted : Monday, 05 November 2012 12:47:00

I’ve run this blog for a while now and definitely see the results of SEO related steps I have taken – especially this one courtesy of Scott Hanselmann http://www.hanselman.com/blog/EmbraceAuthorshipTheImportanceOfRelmeAndRelauthorOnYourContentsSEOAndGoogle.aspx. The downside of these improved search rankings is the amount of spam that comes in via the site. I had my blog redesigned a few months ago and one of the improvements I added was to make posting of comments and emails easier, unfortunately this also makes the link spammers life easier. For some reason about 2 weeks ago my daily blog-spam count went from about 5 to >100 a day, this became a real pain in the a*** for me as I forward every comment I receive to my personal email. I searched around the internet and came across Akismet.Net – its a free library you can use to auto filter spam and stop the blight. There wasn’t a massive amount of help that I found on how to use it but I did find a few bits and pieces that I was able to cobble together – all in all it took about an hour to implement…

1) First I installed Akismet.NET (via Nuget of course)


2) Then registered for a free Akismet API key at http://akismet.com/

3) Then added a couple of extension methods. The first to convert a posted instance of my BlogComment class to an instance of the Akismet Comment class, the second will invoke the Akismet spam checking service. I got this from the following blog post http://blog.cretze.ro/?p=78.

    /// <summary>

    /// This class add some extension methods for the <see cref="BlogComment"/>.

    /// </summary>

    public static class MessageExtensions


        /// <summary>

        /// Convert the input message to an Akismet comment.

        /// </summary>

        /// <param name="message">The input message.</param>

        /// <param name="context">The input <see cref="System.Web.HttpContextBase"/> to use in convertion.</param>

        /// <returns>The corresponding Akismet comment.</returns>

        public static Comment ToComment(this BlogComment message, HttpContextBase context)


            Comment result = new Comment


                // initialize result

                blog = ConfigurationManager.AppSettings["AKISMET_DOMAIN"],

                comment_type = "comment",

                comment_author = message.Author,

                comment_author_email = message.Email,

                comment_content = message.Body,

                permalink = String.Empty,

                referrer = context.Request.ServerVariables["HTTP_REFERER"],

                user_agent = context.Request.ServerVariables["HTTP_USER_AGENT"],

                user_ip = context.Request.ServerVariables["REMOTE_ADDR"]



            // return result


  • (This will not appear on the site)