Skip to content

Commit de2badb

Browse files
authored
Merge pull request #6 from maliming/dev
Support reCAPTCHA v3 programmatically.
2 parents bf59987 + f64fc5c commit de2badb

File tree

8 files changed

+121
-36
lines changed

8 files changed

+121
-36
lines changed

README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ services.AddreCAPTCHAV3(x =>
3939
<recaptcha-script-v3-js action="login" callback="callback" />
4040
```
4141

42+
```
43+
@addTagHelper *, Owl.reCAPTCHA
44+
45+
<form method="POST" id="recaptchaForm">
46+
<input id="token" name="token" type="text" />
47+
<input id="submitBtn" type="submit" value="submit" />
48+
</form>
49+
50+
<script>
51+
document.getElementById("submitBtn").onclick = function(e) {
52+
e.preventDefault();
53+
grecaptcha.reExecute(function(token) {
54+
document.getElementById("token").value = token;
55+
document.getElementById("recaptchaForm").submit();
56+
})
57+
};
58+
</script>
59+
60+
<recaptcha-script-v3 />
61+
<recaptcha-script-v3-js action="login" execute="false" />
62+
```
63+
4264
### v3 razor page model
4365

4466
```
@@ -158,7 +180,7 @@ services.AddreCAPTCHAV2(x =>
158180
public class V2_CheckboxModel : PageModel
159181
{
160182
private readonly IreCAPTCHASiteVerifyV2 _siteVerify;
161-
183+
162184
public V2_CheckboxModel(IreCAPTCHASiteVerifyV2 siteVerify)
163185
{
164186
_siteVerify = siteVerify;
@@ -184,4 +206,4 @@ public class V2_CheckboxModel : PageModel
184206
*/
185207
}
186208
}
187-
```
209+
```

demo/reCAPTCHA.Demo/Pages/Index.cshtml

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
</div>
1515

1616

17+
<div>
18+
<a href="/V3_Programmatically">V3 Programmatically Demo</a>
19+
</div>
20+
21+
1722
<div>
1823
<a href="/V2_Invisible">V2_Invisible Demo</a>
1924
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@page
2+
@using Microsoft.Extensions.Options
3+
@using Owl.reCAPTCHA
4+
@model reCAPTCHA.Demo.Pages.V3_ProgrammaticallyModel
5+
@inject IOptions<reCAPTCHAOptions> Options
6+
@{
7+
ViewData["Title"] = "V3";
8+
}
9+
10+
<pre>@Model.Result</pre>
11+
12+
<form method="POST" id="recaptchaForm">
13+
<input id="token" name="token" type="text" />
14+
<input id="submitBtn" type="submit" value="submit" />
15+
</form>
16+
17+
<script>
18+
document.getElementById("submitBtn").onclick = function(e) {
19+
e.preventDefault();
20+
grecaptcha.reExecute(function(token) {
21+
document.getElementById("token").value = token;
22+
document.getElementById("recaptchaForm").submit();
23+
})
24+
};
25+
</script>
26+
27+
<recaptcha-script-v3 />
28+
<recaptcha-script-v3-js action="login" execute="false" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Mvc.RazorPages;
3+
using Newtonsoft.Json;
4+
using Owl.reCAPTCHA;
5+
using Owl.reCAPTCHA.v3;
6+
7+
namespace reCAPTCHA.Demo.Pages
8+
{
9+
public class V3_ProgrammaticallyModel : PageModel
10+
{
11+
private readonly IreCAPTCHASiteVerifyV3 _siteVerify;
12+
13+
public string Result { get; set; }
14+
15+
public V3_ProgrammaticallyModel(IreCAPTCHASiteVerifyV3 siteVerify)
16+
{
17+
_siteVerify = siteVerify;
18+
}
19+
20+
public async Task OnPostAsync(string token)
21+
{
22+
var response = await _siteVerify.Verify(new reCAPTCHASiteVerifyRequest
23+
{
24+
Response = token,
25+
RemoteIp = HttpContext.Connection.RemoteIpAddress.ToString()
26+
});
27+
28+
Result = JsonConvert.SerializeObject(response, Formatting.Indented);
29+
}
30+
}
31+
}

demo/reCAPTCHA.Demo/Properties/launchSettings.json

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
{
2-
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
5-
"iisExpress": {
6-
"applicationUrl": "http://localhost:3558",
7-
"sslPort": 44369
8-
}
9-
},
102
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
13-
"launchBrowser": true,
14-
"environmentVariables": {
15-
"ASPNETCORE_ENVIRONMENT": "Development"
16-
}
17-
},
18-
"reCAPTCHA.Tests": {
3+
"reCAPTCHA.Demo": {
194
"commandName": "Project",
205
"launchBrowser": true,
216
"applicationUrl": "https://localhost:5001;http://localhost:5000",

demo/reCAPTCHA.Demo/Startup.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6060
app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
6161
}
6262
}
63-
}
63+
}

src/Owl.reCAPTCHA/Owl.reCAPTCHA.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1717
<!-- including PDB files in NuGet for source link because symbolsource.org does not support portable PDBs -->
1818
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
19+
<PackageVersion>0.3.1</PackageVersion>
1920
</PropertyGroup>
2021

2122
<ItemGroup>

src/Owl.reCAPTCHA/v3/TagHelpers/reCAPTCHAV3ScriptJsTagHelper.cs

+30-17
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,51 @@ public class reCAPTCHAV3ScriptJsTagHelper : TagHelper
1010

1111
public string Callback { get; set; }
1212

13+
public bool Execute { get; set; }
14+
1315
private readonly reCAPTCHAOptions _options;
1416

1517
public reCAPTCHAV3ScriptJsTagHelper(IOptionsSnapshot<reCAPTCHAOptions> optionsAccessor)
1618
{
1719
_options = optionsAccessor.Get(reCAPTCHAConsts.V3);
20+
Execute = true;
1821
}
1922

2023
public override void Process(TagHelperContext context, TagHelperOutput output)
2124
{
2225
/*
23-
grecaptcha.ready(function() {
24-
grecaptcha.reExecute = function(){
25-
grecaptcha.execute('_reCAPTCHA_site_key_', {action: 'homepage'}).then(function(token) {
26-
...
27-
})();
28-
}
26+
myCallback is a user-defined method name or `(function(t){alert(t)})` when Execute = true
27+
grecaptcha.ready(function () {
28+
grecaptcha.reExecute = function () {
29+
grecaptcha.execute('6LccrsMUAAAAANSAh_MCplqdS9AJVPihyzmbPqWa', {
30+
action: 'login'
31+
}).then(function (token) {
32+
myCallback(token)
33+
})
34+
};
35+
grecaptcha.reExecute()
2936
});
30-
*/
3137
38+
myCallback is a user-defined function when Execute = false
39+
grecaptcha.ready(function () {
40+
grecaptcha.reExecute = function (callback) {
41+
grecaptcha.execute('6LccrsMUAAAAANSAh_MCplqdS9AJVPihyzmbPqWa', {
42+
action: 'login'
43+
}).then(myCallback)
44+
};
45+
});
46+
*/
3247
output.TagName = "script";
3348
output.TagMode = TagMode.StartTagAndEndTag;
3449

35-
var script = "grecaptcha.ready(function(){ " +
36-
"grecaptcha.reExecute = function(){" +
37-
"grecaptcha.execute('" + _options.SiteKey + "'" + (string.IsNullOrWhiteSpace(Action) ? "" : ",{action:'" + Action + "'}") + ")" +
38-
".then(function(token){" +
39-
Callback + "(token)" +
40-
"})" +
41-
"};" +
42-
"grecaptcha.reExecute()" +
43-
"});";
50+
var script =
51+
"grecaptcha.ready(function(){ " +
52+
"grecaptcha.reExecute = function(" + (Execute ? "" : "callback") + "){" +
53+
"grecaptcha.execute('" + _options.SiteKey + "'" + (string.IsNullOrWhiteSpace(Action) ? "" : ",{action:'" + Action + "'}") + ")" +
54+
(Execute ? (".then(function(token){" + Callback + "(token)" + "})") : ".then(callback)") + "};" +
55+
(Execute ? "grecaptcha.reExecute()" :"") +
56+
"});";
4457
output.Content.SetHtmlContent(script);
4558
}
4659
}
47-
}
60+
}

0 commit comments

Comments
 (0)