An efficient and secured cross-platform alternative to Google reCAPTCHA for ASP.NET Core, written in C#. If you're tired of struggling with Google reCAPTCHA and worried about performance and banning issues, take a minute to consider this solution. Built with modern .NET 9 and cross-platform ImageSharp technology.
- β Cross-Platform: Works on Windows, Linux, and macOS using SixLabors.ImageSharp
- β .NET 9 Compatible: Built with the latest .NET 9 and modern C# patterns
- β Zero Dependencies: No external services or APIs required
- β Lightweight: Minimal footprint with high performance
- β Customizable: Colors, fonts, sizes, and character types
- β Secure: Session-based verification with configurable timeout
- β Comprehensive Testing: 57+ test cases ensuring reliability
Install the package from NuGet:
dotnet add package EasyCaptchaCore --version 1.0.1
Or via Package Manager Console:
Install-Package EasyCaptchaCore -Version 1.0.1
using EasyCaptcha.Service;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddControllersWithViews();
// Add session support
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
});
// Add EasyCaptcha service
builder.Services.AddTransient<ICaptchaService, CaptchaService>();
var app = builder.Build();
// Configure the HTTP request pipeline
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession(); // Enable session before authorization
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Basic implementation:
<img src="~/Captcha" alt="Captcha" />
With click-to-refresh functionality:
<img id="captchaImage" src="~/Captcha" alt="Captcha"
style="cursor: pointer;" onclick="refreshCaptcha()" />
<script>
function refreshCaptcha() {
const img = document.getElementById('captchaImage');
const timestamp = new Date().getTime();
img.src = `/Captcha?_=${timestamp}`;
}
</script>
[HttpPost]
public IActionResult VerifyCaptcha([FromBody] CaptchaRequest request)
{
var sessionCaptcha = HttpContext.Session.GetString("captcha");
if (string.IsNullOrEmpty(sessionCaptcha) || string.IsNullOrEmpty(request.Captcha))
{
return Json(new { success = false, message = "Invalid captcha" });
}
var isValid = string.Equals(sessionCaptcha, request.Captcha,
StringComparison.OrdinalIgnoreCase);
// Clear session after verification
HttpContext.Session.Remove("captcha");
return Json(new { success = isValid });
}
public class CaptchaRequest
{
public string Captcha { get; set; } = string.Empty;
}
Change the number of characters (default: 5):
<img src="~/Captcha?l=6" alt="Captcha" />
Set background color by name or use "random" (default: "transparent"):
<img src="~/Captcha?bc=white" alt="Captcha" />
<img src="~/Captcha?bc=random" alt="Captcha" />
Set text color by name or use "random" (default: "random"):
<img src="~/Captcha?fc=black" alt="Captcha" />
<img src="~/Captcha?fc=random" alt="Captcha" />
Choose between mixed alphanumeric or numbers only (default: "MIX"):
<img src="~/Captcha?t=NUM" alt="Captcha" /> <!-- Numbers only -->
<img src="~/Captcha?t=MIX" alt="Captcha" /> <!-- Mixed characters -->
Customize width and height (default: 120x40):
<img src="~/Captcha?width=200&height=60" alt="Captcha" />
Specify font family (default: "Arial"):
<img src="~/Captcha?fontFamily=Times%20New%20Roman" alt="Captcha" />
<img src="~/Captcha?l=6&bc=white&fc=blue&t=MIX&width=180&height=50" alt="Captcha" />
- Named Colors: white, black, red, green, blue, yellow, cyan, magenta, gray, transparent
- Random: Use "random" for random color generation
- Transparent: Use "transparent" for transparent background
The project includes comprehensive testing with 57+ test cases:
# Run all tests
dotnet test
# Run tests with coverage
dotnet test --collect:"XPlat Code Coverage"
EasyCaptcha/
βββ EasyCaptcha/ # Main library
β βββ Controllers/ # Captcha controller
β βββ Service/ # Captcha service implementation
β βββ Enum/ # Character type definitions
βββ Sample/ # Demo web application
β βββ Controllers/ # Sample controllers
β βββ Views/ # Sample views
β βββ Models/ # Sample models
βββ XUnitTest/ # Comprehensive test suite
Run the included sample application:
dotnet run --project Sample/Sample.csproj
Features:
- Interactive CAPTCHA verification
- Real-time refresh functionality
- Multiple configuration examples
- AJAX-based form submission
- Modern responsive UI
- .NET 9.0 or later
- ASP.NET Core 9.0 or later
- Session support enabled
- SixLabors.ImageSharp (3.1.6) - Cross-platform image processing
- SixLabors.ImageSharp.Drawing (2.1.5) - Drawing capabilities
- SixLabors.Fonts (2.1.0) - Font support
This project is licensed under the MIT License. See the LICENSE file for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
If you encounter any issues or have questions:
- Create an issue on GitHub
- Check the Sample project for implementation examples
- Review the test cases for usage patterns
- Built with modern .NET 9 and C# patterns
- Cross-platform compatibility thanks to SixLabors.ImageSharp
- Comprehensive testing ensuring reliability
- Community feedback and contributions
Easy Captcha - Making CAPTCHA simple, secure, and cross-platform! π‘οΈβ¨