C# MVC3:通过 HTTPS 加载的网站无法加载 recaptcha 安全代码。 Chrome 调试器给出 "Mixed Content error"

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

我有一个 C# MVC 应用程序,它在特定视图中使用 recaptcha 安全代码。 在我看来,我有以下代码:

<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")

当我尝试加载页面时,我在 chrome 的调试器中收到以下错误:

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.

如果我检查加载页面的来源,recaptcha 的 Razor 控件会生成此脚本标记:

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

src 属性是 http url 而不是 https url。

如果有人知道我如何克服这个错误,将不胜感激。 谢谢, 卡佩塔尼奥斯

好的,我在这里再次发布我自己问题的答案。

原来是这样:

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

正在渲染这个:

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

脚本标签的 src 属性包含 http 而不是 https 所以,为了解决这个问题,我只是用这个替换了@html.raw & @html.validate:

<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>

这样做可以阻止 chrome 调试器收到错误。