Skip to content

arsanjani/Easy-Captcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

33 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Easy Captcha

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.

πŸš€ Features

  • βœ… 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

πŸ“Έ Examples

c1 c2 c3 c4 c5 c6 c7 c8 c9 c10

πŸ› οΈ Installation

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

πŸ”§ Setup (.NET 9)

1. Configure Services in Program.cs

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();

2. Add Captcha to Your View

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>

3. Verify Captcha in Controller

[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;
}

🎨 Customization Options

Length

Change the number of characters (default: 5):

<img src="~/Captcha?l=6" alt="Captcha" />

Background Color

Set background color by name or use "random" (default: "transparent"):

<img src="~/Captcha?bc=white" alt="Captcha" />
<img src="~/Captcha?bc=random" alt="Captcha" />

Foreground Color

Set text color by name or use "random" (default: "random"):

<img src="~/Captcha?fc=black" alt="Captcha" />
<img src="~/Captcha?fc=random" alt="Captcha" />

Character Type

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 -->

Image Size

Customize width and height (default: 120x40):

<img src="~/Captcha?width=200&height=60" alt="Captcha" />

Font Family

Specify font family (default: "Arial"):

<img src="~/Captcha?fontFamily=Times%20New%20Roman" alt="Captcha" />

Combined Example

<img src="~/Captcha?l=6&bc=white&fc=blue&t=MIX&width=180&height=50" alt="Captcha" />

🎯 Supported Colors

  • 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

πŸ§ͺ Testing

The project includes comprehensive testing with 57+ test cases:

# Run all tests
dotnet test

# Run tests with coverage
dotnet test --collect:"XPlat Code Coverage"

πŸ“ Project Structure

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

πŸš€ Demo Application

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

πŸ“‹ Requirements

  • .NET 9.0 or later
  • ASP.NET Core 9.0 or later
  • Session support enabled

πŸ”§ Dependencies

  • SixLabors.ImageSharp (3.1.6) - Cross-platform image processing
  • SixLabors.ImageSharp.Drawing (2.1.5) - Drawing capabilities
  • SixLabors.Fonts (2.1.0) - Font support

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ž Support

If you encounter any issues or have questions:

πŸŽ‰ Acknowledgments

  • 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! πŸ›‘οΈβœ¨

About

An efficient and secured alternative of Google reCAPTCHA for .NET

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages