forked from VladislavAntonyuk/MauiSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
90 lines (79 loc) · 2.78 KB
/
MainPage.xaml.cs
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
namespace MauiCaptcha;
using System.Net.Http.Json;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using Microsoft.AspNetCore.Components.WebView;
public partial class MainPage : ContentPage
{
private const string AndroidSiteKey = "";
private const string AndroidSecretKey = "";
private const string WebSiteKey = "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI";
private const string WebSecretKey = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe";
public MainPage()
{
InitializeComponent();
}
async void OnAndroidCaptchaVerificationClicked(object sender, EventArgs e)
{
#if ANDROID
var api = Android.Gms.SafetyNet.SafetyNetClass.GetClient(Platform.CurrentActivity);
var response = await api.VerifyWithRecaptchaAsync(AndroidSiteKey);
if (response != null && !string.IsNullOrEmpty(response.TokenResult))
{
var captchaResponse = await ValidateCaptcha(response.TokenResult, AndroidSecretKey);
if (captchaResponse is null || !captchaResponse.Success)
{
await Toast.Make($"Invalid captcha: {string.Join(",", captchaResponse?.ErrorCodes ?? Enumerable.Empty<object>())}", ToastDuration.Long).Show();
return;
}
if (Platform.CurrentActivity!.PackageName != captchaResponse.ApkPackageName)
{
await Toast.Make($"Package Names do not match: {captchaResponse.ApkPackageName}", ToastDuration.Long).Show();
}
else
{
await Toast.Make("Success", ToastDuration.Long).Show();
}
}
else
{
await Toast.Make("Failed", ToastDuration.Long).Show();
}
#else
await Toast.Make("This button works only on Android", ToastDuration.Long).Show();
#endif
}
private async void BlazorWebView_OnUrlLoading(object? sender, UrlLoadingEventArgs e)
{
e.UrlLoadingStrategy = UrlLoadingStrategy.OpenInWebView;
var query = System.Web.HttpUtility.ParseQueryString(e.Url.Query);
var token = query.Get("token");
if (!string.IsNullOrEmpty(token))
{
var captchaResponse = await ValidateCaptcha(token, WebSecretKey);
if (captchaResponse is null || !captchaResponse.Success)
{
await Toast.Make($"Invalid captcha: {string.Join(",", captchaResponse?.ErrorCodes ?? Enumerable.Empty<object>())}", ToastDuration.Long).Show();
return;
}
await Toast.Make("Success", ToastDuration.Long).Show();
}
}
static async Task<CaptchaResult?> ValidateCaptcha(string token, string secretKey)
{
using var client = new HttpClient();
var parameters = new Dictionary<string, string>
{
{ "secret", secretKey },
{ "response", token }
};
var content = new FormUrlEncodedContent(parameters);
var response = await client.PostAsync("https://www.google.com/recaptcha/api/siteverify", content);
if (!response.IsSuccessStatusCode)
{
return null;
}
var responseContent = await response.Content.ReadFromJsonAsync<CaptchaResult>();
return responseContent;
}
}