Giter Site home page Giter Site logo

ashleyc2219 / aspnetcore.recaptcha Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaelvs97/aspnetcore.recaptcha

0.0 0.0 0.0 1.72 MB

Google ReCAPTCHA v2/v3 Library for .NET Core 3.x/5.x

Home Page: https://www.nuget.org/packages/AspNetCore.ReCaptcha/

License: MIT License

C# 100.00%

aspnetcore.recaptcha's Introduction

AspNetCore.ReCaptcha

NuGet Version Coverage Status

ReCAPTCHA Library for .NET Core 3.1 and .NET 5.0/6.0.

Requirements

This package requires a secret key as well as a site key provided by ReCaptcha. You can aquire your keyset at https://www.google.com/recaptcha/intro/v3.html. It's possible to use either v2 or v3 ReCaptcha.

Installation

You can install this package using NuGet. You can use the following command:

# Package Manager
PM> Install-Package AspNetCore.ReCaptcha

# .NET CLI
dotnet add package AspNetCore.ReCaptcha

Configuration

Place the aquired secret key and site key in the appsettings.json of your project. An example of the appsettings file is below:

{
    "ReCaptcha": {
        "SiteKey": "your site key here",
        "SecretKey": "your secret key here",
        "Version": "v2", // The ReCaptcha version to use, can be v2, v2invisible or v3
        "UseRecaptchaNet": false, // Value whether to use google recaptcha or recaptcha.net
        "ScoreThreshold": 0.5, // Only applicable for recaptcha v3, specifies the score threshold when it is considered successful
    }
}

Usage

To use AspNetCore.ReCaptcha in your project, you must add the following code to your startup.cs:

public void ConfigureServices(IServiceCollection services) {
    services.AddReCaptcha(Configuration.GetSection("ReCaptcha"));
}

or if you are using the new minimal API in .NET 6.0 add this to your program.cs instead:

builder.Services.AddReCaptcha(builder.Configuration.GetSection("ReCaptcha"));

In your .cshtml file you add the following using statement:

@using AspNetCore.ReCaptcha

And then you can add the ReCaptcha element to your DOM using the following code or make use of the tag-helper:

@Html.ReCaptcha()
<recaptcha />

To be able to make use of the taghelper, you will need to include the following line of code in your _ViewImports.cshtml:

@addTagHelper *, AspNetCore.ReCaptcha

The action that you will be posting to (in this case SubmitForm) will need the following attribute on the method:

[ValidateReCaptcha]
[HttpPost]
public IActionResult SubmitForm(ContactViewModel model)
{
    if (!ModelState.IsValid)
        return View("Index");

    TempData["Message"] = "Your form has been sent!";
    return RedirectToAction("Index");
}

Language support

By default, AspNetCore.ReCaptcha will use the language that is being used in the request. So we will make use of the Culture of the HttpContext. However, you can override this by specifying a language in the ReCaptcha element. This is shown in the next example:

@Html.ReCaptcha(language: "en-GB")
<recaptcha language="en-GB" />

We support all languages supported by ReCaptcha, list can be found here.

This will only change the language of the recaptcha widget that is shown, not the language of the message that is shown when the recaptcha failed.

To localize the error message (e.g. the message set with [ValidateReCaptcha(ErrorMessage = "")]), add the message to the resource file of the controller or razor page that the validate attribute is used. For an example of this check the .NET 6 sample and look at the Pages/ContactModel.cs and Resources/Pages/ContactModel.resx files.

You can learn more about request localization in .NET Core here

Examples

For every supported version of .NET there is a configured example included in this repository. As of .NET 5.0, we support both MVC and Razor pages. The .NET 6.0 example is using the new minimal API. Examples are linked below for quick access:

.NET Core 3.1

.NET 5.0

.NET 6.0

aspnetcore.recaptcha's People

Contributors

sleeuwen avatar michaelvs97 avatar nwoolls avatar bartoszmiz avatar qyl27 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.