问题
I would like to implement captcha in Xamarin android app. I got hold of this integrate googles reCaptcha validation in Android. But its a native android project. I also found this, but I am unable to make it work. I am getting Failed to Check Compatibility
alert. The API SafetyNetClass.SafetyNetApi.AttestAsync
is returning an error. Not sure why.
Is there any other way to go about captcha validation. Any help would be appreciated.
回答1:
In order to use the SafetyNetApi
to do reCaptcha
validation you need to:
- Set your
minSdkVersion
to 14 or higher - Make sure you include
Google Repository
in your SDK tools - Install
Xamarin.GooglePlayServices.SafetyNet
package - Sign your app package name in reCAPTCHA Android signup site and get your Site key and Secret key
After this you launch the verification of the captcha:
try
{
var response = await SafetyNetClass.GetClient(this.context).VerifyWithRecaptchaAsync("YOUR_API_SITE_KEY");
if (!string.IsNullOrEmpty(response.TokenResult))
{
// Validate the user response token using the
// reCAPTCHA siteverify API.
}
}
catch(Exception ex)
{
// Handle exception
throw ex;
}
After you get the token result not null, you need to verify it using the reCAPTCHA siteverify API. Here is where you need to use the Secret key
.
Pay attention to error handling when communicating with the reCaptcha service. You can check more about this following the links below.
More Info:
- https://developer.android.com/training/safetynet/recaptcha#validate-response
- https://developers.google.com/recaptcha/docs/verify
- http://android-er.blogspot.com/2017/06/example-using-safetynet-recaptcha-api.html (careful here because some of the methods used here are obsolete)
来源:https://stackoverflow.com/questions/52165506/recaptcha-in-android-xamarin