Skip to content

peacecwz-archive/Koptcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Koptcha

Koptcha is simple Captcha library for .NET Core Projects

Getting Started

  1. Add Koptcha as a dependency to your .NET Core Projects
PS> Install-Package Koptcha

or

$ dotnet add package Koptcha
  1. Configure settings in Startup.cs file. You have to use a Cache Provider. You can use SQL Server Cache, In Memory Cache or Distributed Cache (like a Redis)
public void ConfigureServices(IServiceCollection services)
{
    services
        .AddDistributedMemoryCache()
        .AddCaptcha();
    
    services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
  1. Configure your appsettings.json file
"Captcha": {
    "DomainName": "Koptcha.API",
    "CaptchaTokenHeaderName": "x-captcha-token",
    "CaptchaControlBaseUrl": "https://www.google.com/recaptcha/api/siteverify?secret={captcha_secretkey}&response={captcha_token}",
    "CaptchaSecretKey": "{captcha_token}",
    "CaptchaSecretKeyHolder": "{captcha_secretkey}",
    "Duration": 60,
    "Threshold": 5, 
    "IsEnabledDuration": true,
    "IsEnabledThreshold": true,
    "IsCaptchaEnabled": true
  } 

How to Use

Use with Action Filter Attribute

IP Based Captcha

  [Captcha(CaptchaType = CaptchaType.Ip, Duration = 5, Threshold = 20)]
  [HttpGet("test1")]
  public IActionResult Get()
  {
      return Ok("Test Ok");
  }

Email Based Captcha

  [Captcha(CaptchaType = CaptchaType.Email, Duration = 5, Threshold = 20, FieldName = "EmailAddress")]
  [HttpPost("test2")]
  public IActionResult GetWithEmail([FromBody]EmailRequest request)
  {
      return Ok($"Test Ok with {request.EmailAddress}");
  }

IP and Email Based Captcha

  [Captcha(CaptchaType = CaptchaType.IpAndEmail, Duration = 5, Threshold = 20, FieldName = "EmailAddress")]
  [HttpPost("test3")]
  public IActionResult GetWithEmailAndIp([FromBody]EmailRequest request)
  {
      return Ok($"Test Ok with {request.EmailAddress}");
  }

Custom Field Captcha

  [Captcha(CaptchaType = CaptchaType.Custom, Duration = 5, Threshold = 20, FieldName = "Username")]
  [HttpPost("test4")]
  public IActionResult GetWithUsername([FromBody]EmailRequest request)
  {
      return Ok($"Test Ok with {request.Username}");
  }

Use with Dependency Service

Coming Soon...

TODO

  • Documentation
  • Unit Testing
  • API Test Project
  • MVC Test Project

License

This project is licensed under the MIT License