问题
I use jQuery-realperson plugin. However, the validate of captcha value at server-side, always fails. I saw similar question at SO, but it was about problem that happen on PHP. I need C# code.
My client side - js, based on knoskout.js:
var validCaptcha = $.ajax({
url: global.webApiConfig.getApiPath('Login/CheckCaptcha'),
data: { Value: $('#captcha').data().realperson.hash, PersonValue: $('#captcha').val() },
type: 'POST',
cache: false,
async: false
});
if (validCaptcha.responseText != true.toString()) {
vm.captchaError('invalid value');
refreshCaptcha();
return;
}
My server side - c#:
[HttpPost]
public bool CheckCaptcha(CaptachaData dataForCheck)
{
int hash = 5381;
dataForCheck.Value = dataForCheck.Value.ToUpper();
for (int i = 0; i < dataForCheck.Value.Length; i++)
{
hash = ((hash << 5) + hash) + dataForCheck.Value[i];
}
return dataForCheck.PersonValue != null && hash == dataForCheck.PersonValue.GetHashCode();
}
public class CaptachaData
{
public string Value { get; set; }
public string PersonValue { get; set; }
}
回答1:
I think I found the mistake! You get hash code of orignial-value- twice! once - at client side:
$('#captcha').data().realperson.hash
and once- at server side:
dataForCheck.PersonValue.GetHashCode()
you need to change at server side:
return dataForCheck.PersonValue != null && hash.ToString() == dataForCheck.PersonValue;
来源:https://stackoverflow.com/questions/35146306/c-sharp-jquery-real-person-allways-fails