Anyone looking for ASP.NET-MVC
solution:
Create a helper class to gather a list of all the known bots. For the above case, the bot is Google-PageRenderer
. The other list is of the well-known bots:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyProject.Helpers
{
public static class BotDetectionHelper
{
private static readonly HashSet<string> Bots = new HashSet<string>(StringComparer.Ordinal)
{
"Google-PageRenderer",
"Googlebot",
"Bingbot",
"Slurp",
"DuckDuckBot",
"Baiduspider",
"YandexBot",
"Sogou",
"Exabot",
"facebookexternalhit",
"LinkedInBot",
"Twitterbot",
"Pinterestbot",
"WhatsApp",
"DotBot",
"spbot/",
"James BOT",
"baidu",
"Baidu",
"/bot",
"semantic-visions.com",
"spider",
"lipperhey",
"linkdexbot/",
"MJ12bot/",
"Lipperhey-Kaus-Australis/",
"BDCbot",
"AhrefsBot",
"SemrushBot",
"Alexa",
"Uptimebot",
"Crawl",
"Spider",
"PageSpeed",
"ZoominfoBot",
"Adidxbot",
"BLEXBot",
"SEOkicks",
"BlackWidow",
"BotALot",
"Buddy",
"BuiltWith",
"Curl",
"DISCo",
"Dotbot",
"Feedfetcher-Google",
"Geekbot",
"GrapeshotCrawler",
"GT::WWW",
"HTTP::Lite",
"HubSpot",
"ia_archiver",
"Jetbot",
"JetBrains Omea Reader",
"Mechanize",
"NetcraftSurveyAgent",
"Nutch",
"Outbrain",
"Python-urllib",
"rogerbot",
"ShowyouBot",
"SiteExplorer",
"Slackbot",
"Teoma",
"Twingly Recon",
"Via",
"Wget",
"Xenu Link Sleuth",
"ZmEu"
};
public static bool IsBotAgent(string cBot) =>
Bots.Any(bot => cBot.IndexOf(bot, StringComparison.Ordinal) != -1);
}
}
Then create a method in your Controller
class:
private bool BotDetectionResult()
{
bool isBot = false;
string userAgent = Request.UserAgent;
try
{
if (userAgent != null)
{
isBot = BotDetectionHelper.IsBotAgent(userAgent);
}
}
catch (Exception ex)
{
//Log exception
}
return isBot;
}
Usage:
bool detectBot = BotDetectionResult();
if (!detectBot)
{
//Send to database etc.
}
If the bots list can be updated to include other well known bots, then please feel free to edit the answer and add your entries.