This project is read-only.

System.ArgumentException: The key is to update incorrect is thrown when user refreshes the captcha image and application is restarted


I'm using MVC4 version of Captha MVC.
Steps to reproduce:
  1. User loads the view containing the Capthca control. The image is rendered correctly and everything looks fine.
  2. The application gets restarted by touching the web.config file (nothing related to Captcha MVC)
  3. The user attempts to refresh the Captcha image by pressing the relevant button or tries to perform the action that would cause a POST to an action on the server.
System.ArgumentException: The key is to update incorrect.
   at CaptchaMvc.Infrastructure.DefaultCaptchaManager.Update(IParameterContainer parameterContainer) in g:\CodePlex\Captcha\CaptchaMvc(Mvc 3)\Infrastructure\DefaultCaptchaManager.cs:line 603
   at CaptchaMvc.Controllers.DefaultCaptchaController.Refresh() in g:\CodePlex\Captcha\CaptchaMvc(Mvc 3)\Controllers\DefaultCaptchaController.cs:line 50
   at lambda_method(Closure , ControllerBase , Object[] )
   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__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()


mstavrev wrote Sep 24, 2014 at 11:54 AM

The problem arises from the fact that the application is currently using InProc session manager. When it gets restarted (due to the web.config change), the session store is cleared. This however can also happen with another session state manager, when the session times out and gets purged from the store - when the user waits long enough on the Captcha page without doing anything and then decides to refresh the captcha image or submit the form.

VVS0205 wrote Sep 24, 2014 at 1:36 PM

Hi there,
Thanks for a great explanation of the problem.
I will change this behavior in the next version.
As a workaround, you can use the CookieStorageProvider, this provider does not depend on session:
protected void Application_Start()
     CaptchaUtils.CaptchaManager.StorageProvider = new CookieStorageProvider();
Vyacheslav Volkov

mstavrev wrote Sep 25, 2014 at 11:20 AM

I understand. I was thinking about using the Cookie provider instead, but I wasn't sure about the following.
Can I use the CookieStorageProvider and also have multiple Captchas on the same page? I'm using this technique right now.

VVS0205 wrote Sep 25, 2014 at 3:54 PM

Yes, you can use this approach with CookieStorageProvider, it will work without problems.

Vyacheslav Volkov

slown1 wrote Jun 13, 2015 at 10:27 PM


It doesn't work at all with CaptchaUtils.CaptchaManager.StorageProvider = new CookieStorageProvider();
and If we use the GetCaptchaManager method it only works the first time.
Do you have some suggestions?