-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCounterV3.razor
90 lines (71 loc) · 3.08 KB
/
CounterV3.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@page "/counterV3"
@using GoogleCaptchaComponent.Services
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@inject IRecaptchaService RecaptchaService
<h1>Counter</h1>
<h3 class="text-danger">@errorMessage</h3>
<h5 class="text-success">@captchaResponse</h5>
<p>Current count: @currentCount</p>
<GoogleRecaptcha
@ref="captcha"
SuccessCallBack="SuccessCallBack"
TimeOutCallBack="TimeOutCallBack"
ServerValidationErrorCallBack="ServerSideValidationError"
ServerSideValidationHandler="ServerSideValidationHandler"
Version="CaptchaConfiguration.Version.V3"
Theme="CaptchaConfiguration.Theme.Dark"
Language="CaptchaLanguages.EnglishUK"
DefaultAction="DefaultTestAction"
RenderOnLoad="false"
>
</GoogleRecaptcha>
<div class="form-group">
<label for="actionName">Action</label>
<input type="text" id="actionName" @bind-value="Action" class="form-control" />
<small id="actionHelp" class="form-text text-muted">The Captcha V3 action name</small>
</div>
<button class="btn btn-primary" @onclick="IncrementCount" disabled="@Disabled">Click me</button>
@code {
void TimeOutCallBack(CaptchaTimeOutEventArgs e)
{
Disabled = true;
Console.WriteLine($"Captcha Time Out with message {e.ErrorMessage}");
errorMessage = $"Captcha Timeout: {e.ErrorMessage}";
}
void SuccessCallBack(CaptchaSuccessEventArgs e)
{
Disabled = false;
captchaResponse = e.CaptchaResponse;
//Make your web call to your backend with your action, then validate the captcha response
base.StateHasChanged();
}
GoogleRecaptcha captcha;
private string errorMessage;
private string captchaResponse;
private int currentCount = 0;
private bool Disabled = false;
private string Action = "/CountUp";
private async Task IncrementCount()
{
Disabled = true;
//Calls the captcha on the action execution
await captcha.ExecuteV3WithActionAsync(Action);
currentCount++;
}
private void ServerSideValidationError(CaptchaServerSideValidationErrorEventArgs e)
{
errorMessage = $"Captcha server side validation error: {e.ErrorMessage}";
}
/// <summary>
/// Captcha Verification Should be done by an internal api which holds the secret key
/// </summary>
/// <returns></returns>
private async Task<ServerSideCaptchaValidationResultModel> ServerSideValidationHandler(ServerSideCaptchaValidationRequestModel requestModel)
{
using var httpClient = new HttpClient();
// var apiResult = await httpClient.GetFromJsonAsync<GoogleCaptchaCheckResponseResult>($"https://localhost:7113/VerifyCaptcha?token={requestModel.CaptchaResponse}");
return new ServerSideCaptchaValidationResultModel(true, "Success");
// return new ServerSideCaptchaValidationResultModel(apiResult.Success, string.Join("\n",apiResult.ErrorCodes ?? new List<string>(){"No Error"}));
}
}