How to use Captcha service

My form post contains my input and a captcha string, which is probably encoded.

     var expectedText = "UqNl" //This is the user input
     var actualText = "rTHcnLsmFspjscj8ABHV/g==" //Is this the actualText? Because they never will be the same...

If I do:

     Syncfusion.JavaScript.CaptchaService.IsValid(input, secret, false);

I get a NullReferenceException:

   at Syncfusion.JavaScript.CaptchaService.IsValid(String expectedText, String actualText, Boolean caseSensitive)
   at AGF.Azure.Web.Controllers.AccountController.Subscribe(SubscriptionForm form) in C:\SRC\MyProject\Controllers\AccountController.cs:line 99
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at Castle.Proxies.AsyncControllerActionInvokerProxy.EndInvokeActionMethod_callback(IAsyncResult asyncResult)
   at Castle.Proxies.Invocations.AsyncControllerActionInvoker_EndInvokeActionMethod.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed()
   at Glimpse.Mvc.AlternateType.AsyncActionInvoker.EndInvokeActionMethod.NewImplementation(IAlternateMethodContext context)
   at Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.AsyncControllerActionInvokerProxy.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__11_0()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

3 Replies

KV Karthikeyan Viswanathan Syncfusion Team August 16, 2018 07:12 AM UTC

Hi Wouter,  
For validation, You should to get the encryptedText details from refresh handler because of the control reset the encrypted Text during the form post.  
Please refer the below code example: 

public static string expectedValue { get; set; } 
[HttpPost] 
public ActionResult CaptchaFeatures(FormCollection Values) 
{ 
    try 
    {    
        if (!CaptchaService.IsValid(expectedValue, Values["validateText"], true)) 
            ModelState.AddModelError("myCaptcha", "Invalid characters. Try again!"); 
        return View(); 
    } 
    catch(Exception e) { 
        return View(e); 
    } 
} 
public ActionResult Refresh(CaptchaParams parameters) 
{ 
    expectedValue = parameters.CaptchaModel.EncryptedText; 
    return parameters.CaptchaActions(); 
} 
 
If you need to prevent this refresh then you can perform refresh action upon clicking the refresh button. kindly refer to the following KB link: https://www.syncfusion.com/kb/8943/how-to-overcome-the-issue-captcha-in-form-post-validation-is-always-being-false 


Refer the syntax for IsValid method  

 

 
Regards,  
Karthikeyan V.  



WO Wouter January 24, 2019 11:27 AM UTC

Its the web, every request is a new instance, so thats not the answer.

Found the answer myself:

    if (!Syncfusion.JavaScript.CaptchaService.IsValid(requestForm["captcha"], requestForm["validateText"], true))
    {
         ModelState.AddModelError(string.Empty, "You have not confirmed that you are not a robot");
    }

Also, why does this forum use reCaptcha when you have your own solution?


PO Prince Oliver Syncfusion Team January 28, 2019 06:12 AM UTC

Hi Wouter, 

Thank you for sharing your solution with us.  

We have been using reCaptcha in forum module earlier than Syncfusion developed the Captcha for Web platforms. Hence, we couldn’t use our own captcha in our forum at the time of Website implementation. We have planned to use our own captcha in our forum pages. This will be considered in our any of upcoming website releases. 

Regards, 
Prince 


Loader.
Up arrow icon