Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled exception while using reCAPTCHA.AspNetCore v3 #46

Closed
shelland opened this issue May 4, 2020 · 8 comments
Closed

Unhandled exception while using reCAPTCHA.AspNetCore v3 #46

shelland opened this issue May 4, 2020 · 8 comments
Assignees
Labels

Comments

@shelland
Copy link

shelland commented May 4, 2020

Installed version: 3.0.0
Project type: netcoreapp3.1

ArgumentNullException: Value cannot be null. (Parameter 'objectToConvert')

reCAPTCHA.AspNetCore.Templates.RecaptchaV3HiddenInputBase+ToStringInstanceHelper.ToStringWithCulture(object objectToConvert)
reCAPTCHA.AspNetCore.Templates.RecaptchaV3HiddenInput.TransformText()
reCAPTCHA.AspNetCore.RecaptchaHelper.Recaptcha(IHtmlHelper helper, RecaptchaSettings settings, T model)
AspNetCore.Views_SupplierRequest_Index.ExecuteAsync() in Index.cshtml
+
@(Html.Recaptcha(recaptchaSettings.Value))

appSettings.config:

"RecaptchaSettings": {
"SecretKey": "some_key",
"SiteKey": "some_key",
"Version": "v3" // tried to remove this line but exception is the same
},

@radl97
Copy link

radl97 commented May 4, 2020

It seems that from 2.2.5 to 3.0.0 the syntax changed:

@(Html.Recaptcha(recaptchaSettings.Value))

should be

@(Html.Recaptcha<RecaptchaV3HiddenInput>(recaptchaSettings.Value))

I think.

See this part of README for more info.

@enemaerke
Copy link

Just hit the exact same issue :)

Could this be related to some culture/resource file thing or a new behavior around stringifying things? It seems, from quickly looking into the Templates file, that it fails in a null check here (https://github.com/TimothyMeadows/reCAPTCHA.AspNetCore/blob/master/reCAPTCHA.AspNetCore/Templates/RecaptchaV3HiddenInput.cs#L307) so maybe the new stuff requires you to always provide language and actions?

@shelland
Copy link
Author

shelland commented May 4, 2020

@radl97 Yep, I know. I'm using it exactly in the same way as it described in documentation including version type parameters.

@enemaerke
Copy link

Not sure if this helps, but my error was incorrectly reading from the appSettings/appConfig. Once I got that sorted, I no longer hit this issue

@TimothyMeadows
Copy link
Owner

TimothyMeadows commented May 4, 2020

Will look into this today as time permits. It's likely it might be related to Action not being supplied. The new version moved from using functional programming to more model based. In the event it's missing a value you can always specify a constructed model yourself here:

@(Html.Recaptcha<RecaptchaV3HiddenInput>(recaptchaSettings.Value, MyRecaptchaV3HiddenInputModel))

@TimothyMeadows TimothyMeadows self-assigned this May 4, 2020
@TimothyMeadows
Copy link
Owner

TimothyMeadows commented May 4, 2020

Looks like this is from 2 reasons: The second reason seems to have been resolved but will add it here anyway just for someone reading this.

The first is there is no default for Actions in V3 when it should be defaulted to "homepage". This will be resolved in the next version. You can fix this now by specifying a constructed model yourself rather than depending on the class model defaults.

The second issue is the original code you posted here is not using T to specify your version. I know you resolved this but this is likely be the most common issue coming from 2 to 3 for users reading. You no long use "Version": "" in JSON to denote which version of recaptcha the library is using. Please check the docs and use the correct model in Html.Recaptcha<T>.

@TimothyMeadows
Copy link
Owner

This should now be resolved in 3.0.1. Please reopen this issue if it persists. Thanks to everyone for assistance provided.

@Chandinipt
Copy link

Hi Tim,
I am hitting this issue in my code.
Installed version: 3.0.10

Controller has it injected:

public ServiceController(RheaWebsiteDB db, IRecaptchaService recaptcha) //injecting the dbcontext { _db = db; this._recaptcha = recaptcha; }

The relevant action code:
[HttpPost] //TODO: Removed this because it doesn't work anymore , Recaptcha.RecaptchaControlMvc.CaptchaValidator] [ValidateRecaptcha] public async Task<ActionResult> RequestAQuote(Quote quote, bool captchaValid, string captchaErrorMessage) { var recaptcha = await _recaptcha.Validate(Request); if (!recaptcha.success) ModelState.AddModelError("captcha", captchaErrorMessage);

The code in the view:

`@using reCAPTCHA.AspNetCore
@using reCAPTCHA.AspNetCore.Versions
@using Microsoft.Extensions.Options

@Inject IOptions RecaptchaSettings

----other stuff

            <div>
                @(Html.Recaptcha<RecaptchaV3HiddenInput>(RecaptchaSettings?.Value))
                
                @Html.ValidationMessage("captcha")
            </div>

`

Have also tried supplying a new RecaptchaV3HiddenInput() object to the Html.recaptcha call. And changing it to version RecaptchaV2Checkbox. But still hitting the ArgumentNullException as shown below:

ArgumentNullException: Value cannot be null. (Parameter 'objectToConvert')
reCAPTCHA.AspNetCore.Templates.RecaptchaV3HiddenInputBase+ToStringInstanceHelper.ToStringWithCulture(object objectToConvert)
reCAPTCHA.AspNetCore.Templates.RecaptchaV3HiddenInput.TransformText()
reCAPTCHA.AspNetCore.RecaptchaHelper.Recaptcha(IHtmlHelper helper, RecaptchaSettings settings, T model)
AspNetCore.Areas_Services_Views_Service_RequestAQuote.ExecuteAsync() in RequestAQuote.cshtml
+
@(Html.Recaptcha(RecaptchaSettings.Value, new RecaptchaV3HiddenInput()))

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants