Skip to content

Commit 2c011e1

Browse files
committed
Updating the README.md.
1 parent 0566a4a commit 2c011e1

File tree

1 file changed

+77
-29
lines changed

1 file changed

+77
-29
lines changed

README.md

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# ASP.NET Core reCAPTCHA
2-
A Google reCPATCHA validation wrapper service for ASP.NET Core. With only a few simple setup steps you are ready to block bots from filling in and submitting forms on your website.
1+
# ASP.NET Core reCAPTCHA
2+
A Google reCAPTCHA validation wrapper service for ASP.NET Core. In only a few simple steps, you are ready to block bots from filling in and submitting forms on your website with reCAPTCHA.
33

4-
This package also supports reCAPTCHA V3, but at the moment does not provide any frontend tag helpers for that. So only backend validation is supported at the moment.
4+
The package supports V2 and V3 and comes with tag helpers that make it easy to add challenges to your forms. Also, backend validation is made easy and requires only the use of an attribute in your controllers or actions that should get validated.
55

66
[![Build Status](https://dev.azure.com/griesingersoftware/ASP.NET%20Core%20Recaptcha/_apis/build/status/jgdevlabs.aspnetcore-recaptcha?branchName=master)](https://dev.azure.com/griesingersoftware/ASP.NET%20Core%20Recaptcha/_build/latest?definitionId=17&branchName=master)
77
[![Build Status](https://vsrm.dev.azure.com/griesingersoftware/_apis/public/Release/badge/f9036ec9-eb1c-4aff-a2b8-27fdaa573d0f/1/2)](https://vsrm.dev.azure.com/griesingersoftware/_apis/public/Release/badge/f9036ec9-eb1c-4aff-a2b8-27fdaa573d0f/1/2)
@@ -13,16 +13,19 @@ This package also supports reCAPTCHA V3, but at the moment does not provide any
1313

1414
Install via [NuGet](https://www.nuget.org/packages/Griesoft.AspNetCore.ReCaptcha/) using:
1515

16-
``PM> Install-Package Griesoft.AspNetCore.ReCaptcha``
16+
`PM> Install-Package Griesoft.AspNetCore.ReCaptcha`
1717

1818
## Quickstart
1919

20-
The first thing you need to do is to sign up for a new API key-pair for your project. You can follow [Google's guide](https://developers.google.com/recaptcha/intro#overview), if you haven't done that yet.
20+
### Prequisites
21+
You will need an API key pair which can be acquired by [signing up here](http://www.google.com/recaptcha/admin). For assistance or other questions regarding that topic, refer to [Google's guide](https://developers.google.com/recaptcha/intro#overview).
2122

22-
After sign-up you should now have a **Site key** and a **Secret key**. Make note of those, you will need them for the next step.
23+
After sign-up, you should have a **Site key** and a **Secret key**. You will need those to configure the service in your app.
2324

2425
### Configuration
2526

27+
#### Settings
28+
2629
Open your `appsettings.json` and add the following lines:
2730

2831
```json
@@ -31,41 +34,57 @@ Open your `appsettings.json` and add the following lines:
3134
"SecretKey": "<Your secret key goes here>"
3235
}
3336
```
37+
**Important:** The `SiteKey` will be exposed to the public, so make sure you don't accidentally swap it with the `SecretKey`.
3438

35-
Make sure to place your site & secret key in the right spot. You risk to expose your secret key to the public if you switch it with the site key.
39+
#### Service Registration
3640

37-
For more inforamtion about ASP.NET Core configuration check out the [Microsoft docs](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1).
41+
Register this service by calling the `AddRecaptchaService()` method which is an extension method of `IServiceCollection`. For example:
3842

39-
In your `Startup.cs` you now need to add the service. Add the following line `services.AddRecaptchaService();` into the `ConfigureServices(IServiceCollection services)` method, like this for excample.
43+
##### .NET 6
44+
45+
```csharp
46+
var builder = WebApplication.CreateBuilder(args);
47+
48+
builder.Services.AddRecaptchaService();
49+
```
50+
51+
##### Prior to .NET 6
4052

4153
```csharp
4254
public void ConfigureServices(IServiceCollection services)
4355
{
44-
// Add other services here
45-
4656
services.AddRecaptchaService();
47-
48-
// Add other services here
4957
}
5058
```
5159

52-
### Adding a reCAPTCHA element on your view
60+
### Adding a reCAPTCHA element to your view
5361

54-
First you will need to import the tag helpers. Open your `_ViewImports.cshtml` file and add the following lines:
62+
First, import the tag helpers. Open your `_ViewImports.cshtml` file and add the following lines:
5563

5664
```razor
5765
@using Griesoft.AspNetCore.ReCaptcha
58-
5966
@addTagHelper *, Griesoft.AspNetCore.ReCaptcha
6067
```
6168

62-
Now you are ready to use the tag helpers in your views. Always add the `<recaptcha-script>` tag on the bottom of your view. This will render the script tag which will load the reCAPTCHA.js API.
69+
Next, you need to add the `<recaptcha-script>` to every view you intend to use the reCAPTCHA. That will render the API script. Preferably you would add this somewhere close to the bottom of your body element.
6370

64-
Next you only need to add a `<recaptcha>` tag in your form and you are all set. This is the most simplest way of adding reCAPTCHA to your views. Now you only need to add backend validation to the controller of your view.
71+
Now you may add a reCAPTCHA challenge to your view where ever you need it. Using the `<recaptcha />` tag in your form will render a reCAPTCHA V2 checkbox inside it.
72+
73+
For invisible reCAPTCHA use:
74+
```html
75+
<button re-invisible form-id="yourFormId">Submit</button>
76+
```
77+
78+
For reCAPTCHA V3 use:
79+
```html
80+
<recaptcha-v3 form-id="yourFormId" action="submit">Submit</recaptcha-v3>
81+
```
6582

6683
### Adding backend validation to an action
6784

68-
Add a using statement to `Griesoft.AspNetCore.ReCaptcha` in your controller. Next you just need to the `[ValidateRecaptcha]` attribute to the action which is triggered by your form.
85+
Validation is done by decorating your controller or action with `[ValidateRecaptcha]`.
86+
87+
For example:
6988

7089
```csharp
7190
using Griesoft.AspNetCore.ReCaptcha;
@@ -75,25 +94,54 @@ namespace ReCaptcha.Sample.Controllers
7594
{
7695
public class ExampleController : Controller
7796
{
78-
public IActionResult Index()
79-
{
80-
return View();
81-
}
82-
8397
[ValidateRecaptcha]
8498
public IActionResult FormSubmit(SomeModel model)
8599
{
86-
// Will hit the next line only if validation was successfull
100+
// Will hit the next line only if validation was successful
87101
return View("FormSubmitSuccessView");
88102
}
89103
}
90104
}
91105
```
106+
Now each incoming request to that action will be validated for a valid reCAPTCHA token.
107+
108+
The default behavior for invalid tokens is a 404 (BadRequest) response. But this behavior is configurable, and you may also instead request the validation result as an argument to your action.
109+
110+
This can be achieved like this:
111+
112+
```csharp
113+
[ValidateRecaptcha(ValidationFailedAction = ValidationFailedAction.ContinueRequest)]
114+
public IActionResult FormSubmit(SomeModel model, ValidationResponse recaptchaResponse)
115+
{
116+
if (!recaptchaResponse.Success)
117+
{
118+
return BadRequest();
119+
}
120+
121+
return View("FormSubmitSuccessView");
122+
}
123+
```
124+
125+
In case you are validating a reCAPTCHA V3 token, make sure you also add an action name to your validator.
126+
127+
For example:
128+
129+
```csharp
130+
[ValidateRecaptcha(Action = "submit")]
131+
public IActionResult FormSubmit(SomeModel model)
132+
{
133+
return View("FormSubmitSuccessView");
134+
}
135+
```
136+
137+
## Options & Customization
92138

93-
Now if validation would fail, the action method would never get called.
139+
There are global defaults that you may modify on your application startup. Also, the appearance and position of V2 tags may be modified. Either globally or each tag individually.
94140

95-
You can configure that behaviour and a lot of other stuff globally at startup or even just seperatly for each controller or action.
141+
All options from the [official reCAPTCHA docs](https://developers.google.com/recaptcha/intro) are available to you in this package.
96142

97-
### Addition information
143+
## Detailed Documentation
144+
Is on it's way...
98145

99-
For more detailed usage guides check out the wiki. You can find guides about additional configuration options, response validation behaviour, explicit rendering of tags, invisible reCAPTCHA elements and the usage of reCAPTCHA V3.
146+
## Contributing
147+
Contributing is heavily encouraged. :muscle: The best way of doing so is by first starting a discussion about new features or improvements you would like to make. Or, in case of a bug, report it first by creating a new issue. From there, you may volunteer to fix it if you like. 😄

0 commit comments

Comments
 (0)