Google ReCaptcha v2 in native Xamarin.Forms apps
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
For Android we are using the native SafetyNet reCAPTCHA API. You only need the site key. As simple as that.
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!