Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/henriqb/recaptcha-asp-net
Google ReCaptcha for Asp Net, simplified
https://github.com/henriqb/recaptcha-asp-net
aspnet c-sharp google-recaptcha recaptcha-asp-net
Last synced: about 13 hours ago
JSON representation
Google ReCaptcha for Asp Net, simplified
- Host: GitHub
- URL: https://github.com/henriqb/recaptcha-asp-net
- Owner: henriqb
- License: mit
- Created: 2015-08-02T20:46:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-10-14T23:41:08.000Z (about 5 years ago)
- Last Synced: 2025-01-01T21:12:10.690Z (8 days ago)
- Topics: aspnet, c-sharp, google-recaptcha, recaptcha-asp-net
- Language: C#
- Size: 193 KB
- Stars: 56
- Watchers: 6
- Forks: 23
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ReCaptcha-Asp-Net
Google ReCaptcha for Asp Net, simplified## Installation
Nuget Url:
https://www.nuget.org/packages/ReCaptcha-AspNet```
PM > Install-Package ReCaptcha-AspNet
```## Configuration
Get your secret and public keys on https://www.google.com/recaptcha/admin
Add to you App/Web.config inside
```xml```
Optional if you want to alter the default language of the Captcha (Get the language code on ReCaptcha site: https://developers.google.com/recaptcha/docs/language)
Or you can use value "Auto" and it will get the Language from System.Thread.CurrentCulture
```xml```
Or via C# code:
It's only needed to call it once, a good place to put this is at Application_Start() function
```C#
string publicKey = "...[public-key]";
string secretKey = "...[secret-key]";
ReCaptcha.Configure(publicKey, secretKey);// Optional, select a default language:
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage.German;
ReCaptcha.Configure(publicKey, secretKey, defaultLanguage);//Auto-select language from System.Thread.CurrentCulture
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage.Auto;
ReCaptcha.Configure(publicKey, secretKey, defaultLanguage);
```Optional if you want to alter the default theme from light to dark mode.
```xml```
Or via C# code:
```C#
//Optional select a default dark theme
ReCaptchaLanguage defaultLanguage = ReCaptchaLanguage.Auto;
ReCaptcha.Configure(publicKey, secretKey, defaultLanguage, ReCaptchaTheme.dark);
```## How to use
### Client Side (v2):
Inside your form
```html
@ReCaptcha.GetCaptcha()```
Optional if you want to override your configured default language:
```html
@ReCaptcha.GetCaptcha(ReCaptchaLanguage.PortugueseBrazil)```
Optional if you want to override your configured theme (light/dark):
```html
@ReCaptcha.GetCaptcha(theme: ReCaptchaTheme.dark)```
### Client Side (Invisible):
Inside your form
```html
function submit() { $('form').submit(); }
@ReCaptcha.GetInvisibleCaptcha("submit", "Save")```
Optional if you want to override your configured default language:
```html
function submit() { $('form').submit(); }
@ReCaptcha.GetInvisibleCaptcha("submit", "Save", ReCaptchaLanguage.PortugueseBrazil)```
### Server Side:
Inside your controller function or in a filter
```C#
string userResponse = HttpContext.Request.Params["g-recaptcha-response"];
bool validCaptcha = ReCaptcha.ValidateCaptcha(userResponse);
if (validCaptcha){
// Real User, validated !
DoStuff();
...
} else {
// Bot Attack, non validated !
return RedirectToAction("YouAreARobot", "Index");
}
```#### Optional: Proxy
You may use a Proxy to send user Response to ReCaptcha Server
```C#
...
const string proxyIp = "xxx.xxx.xxx.xxx";
const int proxyPort = 1234;
WebProxy webProxy = new WebProxy(proxyIp, proxyPort);
bool validCaptcha = ReCaptcha.ValidateCaptcha(userResponse, webProxy);
...
```May throws the following exception, if the secret key is invalid, or you pass a invalid user response as the ValidateCaptcha parameter:
```C#
throw new ReCaptchaException();
```It can also be called async:
```C#
public async ActionResult MyFunction() {
string userResponse = HttpContext.Request.Params["g-recaptcha-response"];
var validCaptcha = ReCaptcha.ValidateCaptchaAsync(userResponse);
DoSomeParallelStuff();
if (await validCaptcha){
// Real User, validated !
DoStuff();
...
} else {
// Bot Attack, non validated !
return RedirectToAction("YouAreARobot", "Index");
}
}
```