djmarquette
djmarquette

Reputation: 812

HttpContext.Request.Browser.IsMobileDevice not working on Production only?

I have a fairly standard MVC website Contact page that includes ReCaptcha to weed out bots. All is working perfectly. I added mobile pages to the site and due to visibility concerns currently wish to omit the recaptcha validation in the controller for the mobile pages only.

Locally this works perfectly, as I check for invalid captcha, and if NOT a mobile device I add error to the model and catch in Catch block. If mobile don't worry about captcha, just validate the model.

    [HttpPost, RecaptchaControlMvc.CaptchaValidator]
    [ValidateAntiForgeryToken]
    public ActionResult Submit(ContactFormViewModel viewModel, bool captchaValid, string captchaErrorMessage)
    {
        try
        {
            if (!captchaValid)
                if (!HttpContext.Request.Browser.IsMobileDevice)
                    ModelState.AddModelError("captcha", captchaErrorMessage);

            if (ModelState.IsValid)
            {
                if (viewModel.Save(viewModel))
                    return RedirectToAction("Thanks");
            }
            return View("Create");
        }
        catch (Exception ex)
        { //. . . error processing
        }

As above, this works perfect locally against both Safari UserAgent set to iPhone, iPad, etc. as well as Opera emulator for a variety of phones. Yet when I deploy to Production, I contantly get an error thrown on Submit with my Captcha error displayed. To me this means the .IsMobileDevice test failed.

Upvotes: 1

Views: 2157

Answers (1)

djmarquette
djmarquette

Reputation: 812

OK - so here is how I "solved" my problem. I thought it might have something to do with the Recaptcha parameters being passed to the "Submit" object used by the standard web form, so for the mobile version I modified the form submit to call a "MobileSubmit" method that doesn't expect anything Recaptcha related.

It turned out simply as:

    //
    // POST: /Contact/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult MobileSubmit(ContactFormViewModel viewModel)
    {
        try
        {
            if (ModelState.IsValid)
            {
                if (viewModel.Save(viewModel))
                    return RedirectToAction("Thanks");
            }
            return View("Create");
        }
        catch (Exception ex)
        { . . . 
         }

And then the call from the mobile view was simply:

@using (Html.BeginForm("MobileSubmit", "Contact", null, FormMethod.Post, new { data_ajax = "false" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Contact</legend>
        @Html.Partial("_ContactFormPartial")

        <br />
        <input type="submit" value="Submit" />
        &nbsp;
        <input type="reset" value="Reset" />
    </fieldset>
}

So still not sure why IsMobile call wouldn't work originally, but am surmising that the addition of the Recaptcha params was causing a side-effect.

Cheers, Dan

Upvotes: 1

Related Questions