Skip to content

RecaptchaDotnet is a simple and easy to use recaptcha verification for asp.net core and .net apps.

License

Notifications You must be signed in to change notification settings

HesamKashefi/RecaptchaDotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RecaptchaDotnet

A simple, easy to use library for Google Recaptcha verification in .net apps specially ASP.NET Core.

Build Status

build

Installation

With dotnet cli

dotnet add package RecaptchaDotnet

Or with nuget package manager console

Install-Package RecaptchaDotnet

How to use

Configure services

In yuour Startup.cs's ConfigureServices method add the following

public void ConfigureServices(IServiceCollection services) 
{
    services.AddRecaptcha(config => 
    {
        // your secret code from google
        config.Secret = "YOUR_SECRET_CODE";
        
        //configure reaction to unsuccessful verifications
        config.InvalidRecaptchaResponseMode = InvalidRecaptchaResponseMode.ReturnBadRequest;
    })
}

Recaptcha Attribute

Add ReCaptchaAttribute to your actions or controllers to enable recaptcha for it

[ApiController]
[Route("[controller]/[action]")]
public class AccountController : Controller
{
    ...

    [HttpPost]
    [AllowAnonymous]
    [ServiceFilter(typeof(ReCaptchaAttribute))]
    public async Task<IActionResult> Login([FromBody] LoginViewModel model)
    {
        ...
    }
}

Recaptcha Middleware

You can also use Recaptcha Middleware to enable recaptcha for every request

public void Configure(IApplicationBuilder app) 
{
    // Add this before Useing mvc and Authentication middlewares
    app.UseRecaptcha();

    app.UseAuthentication();
    app.UseMvcWithDefaultRoute();
}

Client Side

All you need to do is to add a Header with name Recaptcha with the verification code that Google Recaptcha generates for the user after interaction of user.

About

RecaptchaDotnet is a simple and easy to use recaptcha verification for asp.net core and .net apps.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages