C# - jQuery real-person allways fails

心已入冬 提交于 2019-12-21 06:37:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!