這是不夠的只有JS客戶端進行檢查。 你也需要檢查你的後端服務器端。客戶端從谷歌驗證碼服務獲取字符串響應。客戶端將該字符串發送到您的服務器您的服務器將該字符串與私鑰發送到谷歌驗證碼服務,並根據答案決定。
UPDATE: 這裏是碼部分與asp.net網頁API
角控制器
angular
.module('yourApp', ['vcRecaptcha'])
.controller('YourController', YourController);
function YourController($scope, vcRecaptchaService) {
var recaptcha = {
key: "your recaptcha public key",
widgetId: null,
response: null,
setWidget: function(widgetId) { recaptcha.widgetId = widgetId; },
setResponse: function(response) { recaptcha.response = response; },
cbExpiration: function() { recaptcha.response = null; },
reset: function() {
recaptcha.response = null;
vcRecaptchaService.reload(recaptcha.widgetId);
}
};
$scope.recaptcha = recaptcha;
$scope.reset = function() {
$scope.info = undefined;
recaptcha.reset();
};
$scope.sendData = function(form, recaptchaResponse) {
if (form.$invalid) return;
$http.post('/api/endpoint', { data: ..., recaptchaResponse: recaptchaResponse})
.then(...
角視圖
<form name="yourForm" novalidate ng-submit="sendData(yourForm, recaptcha.response)>
...
<input type="hidden" name="recaptchaResponse" ng-model="recaptcha.response" required/>
<div class="recaptcha-container">
<div vc-recaptcha theme="'light'" key="recaptcha.key"
on-create="recaptcha.setWidgetId(widgetId)"
on-success="recaptcha.setResponse(response)"
on-expire="recaptcha.cbExpiration()"></div>
</div>
<span class="recaptcha-error-info" ng-show="yourForm.$submitted && (yourForm.recaptchaResponse.$invalid || yourForm.$error.recaptcha.length)">
Pass the check, please
</span>
...
asp.net網頁API控制器
使用
[RoutePrefix("api")]
public class YourApiController : ApiController
{
[HttpPost]
[Route("endpoint")]
public async Task<IHttpActionResult> Post([FromBody] YourVm model)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var remoteIp = Request.GetOwinContext().Request.RemoteIpAddress;
var recaptchaVerifyResponse = await VerifyAsync(model.RecaptchaResponse, remoteIp);
if (recaptchaVerifyResponse == null || !recaptchaVerifyResponse.Success) return BadRequest("recaptcha error");
...
}
// may be exrtracted into service
private async Task<RecaptchaVerifyResponse> VerifyAsync(string response, string remoteip)
{
const string RecaptchaVerifyUrl = "https://www.google.com/recaptcha/api/siteverify";
string responseString;
using (var client = new HttpClient())
{
var values = new Dictionary<string, string>
{
{"secret", "your recaptcha private key"},
{"response", response},
{"remoteip", remoteip},
};
var content = new FormUrlEncodedContent(values);
var postResponse = await client.PostAsync(RecaptchaVerifyUrl, content);
responseString = await postResponse.Content.ReadAsStringAsync();
}
if (string.IsNullOrWhiteSpace(responseString)) return null;
RecaptchaVerifyResponse result;
try
{
result = JsonConvert.DeserializeObject<RecaptchaVerifyResponse>(responseString);
}
catch (JsonException)
{
return null;
}
return result;
}
}
public class RecaptchaVerifyResponse
{
[JsonProperty("success")]
private bool? _success = null;
public bool Success
{
get { return _success == true; }
}
[JsonProperty("error-codes")]
private string[] _errorCodes = null;
public string[] ErrorCodes
{
get { return _errorCodes ?? new string[0]; }
}
}
來源
2016-04-13 09:15:16
Ali
該怎麼做,請告訴我 – chaitanya
什麼是您的服務器的技術? – Ali
asp.net與web api使用angularjs。這些用於我的要求 – chaitanya