C# MVC3: website loaded over HTTPS cannot load recaptcha security code. Chrome debugger gives a “Mixed Content error”

北慕城南 提交于 2019-12-13 21:17:34

问题


I have a C# MVC app that makes use of recaptcha security code in a particular view. In my view i have the following code:

<script type="text/javascript" src="https://www.google.com/recaptcha/api/js/recaptcha_ajax.js"></script>
...
@Html.Raw(Html.GenerateCaptcha("captcha", "white"))
@Html.ValidationMessage("captcha")

When i try to load the page, i get the following error in chrome's debugger:

Mixed Content: The page at 'https://mywebsite.com' was loaded over HTTPS, but requested an insecure resource 'http://www.google.com/recaptcha/api/challenge?k=mykey'. This request has been blocked; the content must be served over HTTPS.

and if i inspect the source of the loaded page, the razor control for recaptcha generates this script tag:

<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=mykey">

the src attribute is a http url not a https url.

If anyone knows how i can overcome this error, it would be greatly appreciated. Thanks, Kapetanios


回答1:


Ok here i am posting an answer to my own question again.

It turns out that this:

@Html.Raw(Html.GenerateCaptcha("captcha", "white"))
@Html.ValidationMessage("captcha")

was rendering this:

<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=mykey"></script>

The src attribute of the script tag contained http and not https So, to fix this issue, i just replaced the @html.raw & @html.validate with this:

<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=mekey"></script>
            <noscript>
                <iframe src="https://www.google.com/recaptcha/api/noscript?k=mykey" height="300" width="500" frameborder="0"></iframe><br>
                <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea><input name="recaptcha_response_field" value="manual_challenge" type="hidden" />
            </noscript>

Doing this stopped chrome debugger from getting the error.



来源:https://stackoverflow.com/questions/28035402/c-sharp-mvc3-website-loaded-over-https-cannot-load-recaptcha-security-code-chr

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