Google ReCaptcha v2 in native Xamarin.Forms apps

Enrique Ramos Vargas
2 min readMay 2, 2022

Have you ever wondered how to bring the widely used security check Google ReCaptcha to your native Xamarin.Forms apps? Join me building a breath-taking Register User screen 😂

First of all, visit the administrator portal for Google reCAPTCHA and create two new sites, for Android and iOS.

ANDROID

reCAPTCHA site settings for Android. Check reCAPTCHA v2 checkbox, and reCAPTCHA Android checkbox. After that, add your Android app package name.
reCAPTCHA site settings for Android

For Android we are using the native SafetyNet reCAPTCHA API. You only need the site key. As simple as that.

iOS

reCAPTCHA site settings for iOS. Check reCAPTCHA v2 checkbox, and reCAPTCHA Invisible checkbox. After that, add the web domains in which the app will be working. Add localhost or 127.0.0.1 to be able to run the invisible reCAPTCHA on an embedded webview.
reCAPTCHA site settings for iOS

For iOS things get a bit worse. Google does not provide a native library for iOS, so we are using the v2 Invisible web implementation.

To achieve a native-like user experience, we are running this web reCAPTCHA in an invisible embedded WebView, that only will display the challenge if a suspicious activity is detected by Google reCAPTCHA.

Our iOS reCAPTCHA service will instantiate and load an invisible “ReCaptchaWebView” and will wait for the reCAPTCHA token result. The user might be challenged with selection of pictures. My ReCaptchaWebView implementation is based in this Swift library.

Check my implementation in the Github repository (iOS reCAPTCHA service, invisible WebView and html file using the v2 invisible Google reCAPTCHA). To make my “ReCaptchaWebView” be sure to set the HTML file as an “Embedded resource”.

Once you have obtained the reCAPTHCA token, you can verify it in your shared code (in your ViewModel, usually) using this Google endpoint.

https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}

That’s all. I hope you found it handy!

Github Repository

--

--