带有母版页的Google Invisible ReCaptcha无效

时间:2018-02-14 17:13:51

标签: javascript c# asp.net

我试图让Google Invisible ReCaptcha在我们的ASP.NET / C#网络应用中正常运行。使用母版页面和面板。这是网页的顶部:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Login.Master" AutoEventWireup="true" CodeBehind="LoginRetail.aspx.cs"     Inherits="OnlineSpecialOrders.Forms.LoginRetail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="https://www.google.com/recaptcha/api.js" async defer type="text/javascript"></script>
<script type="text/javascript">
    function onSubmit() {
        window.location.href = '~/MainPage.aspx';
    }
</script>
<link rel="stylesheet" type="text/css" href="../Styles/styleSO.css" />
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderPageHeading" runat="server">
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderBody" runat="server">
<div id="divMain" style="margin-left:auto; margin-right:auto" align="center" runat="server">
    <div id="content">
        <div id="mainContent">
            <div id="generalcontent">
                <asp:Panel ID="pnlRetail" runat="server" Width="960px" Visible="true" DefaultButton="loginButton">
                    <div id="Div7" style="border: medium ridge #863C18; width:575px; height:400px; margin-left:auto; margin-right: auto" align="center">
                        ...
                        <div class="g-recaptcha" data-sitekey="THISISOURNONSECRETSITEKEY" data-callback="onSubmit" data-size="invisible"></div>
                        <div style="margin-left: 15px" class="floatL">
                            <asp:Button runat="server" ID="loginButton" Text="Login" CssClass="button brown bigrounded" Width="150px" Height="30px" OnClick="onlineSpecialOrderRetailLogin_Authenticate" UseSubmitBehavior="false" />
                        </div>

这是背后的代码:

private bool ValidateRecaptcha()
    {
        string captcha_secret_key = "THISISOURSECRETKEY";
        string url = @"https://www.google.com/recaptcha/api/siteverify";
        WebRequest request = WebRequest.Create(url);
        string postData = $"secret={captcha_secret_key}&response={Request["grecaptcha-execute"]}&remoteip={Request.UserHostAddress}";

        try {
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;

            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(postData);
            writer.Close();

            StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());
            string responseData = reader.ReadToEnd();

            JavaScriptSerializer jss = new JavaScriptSerializer();
            CaptchaResponse cResponse = jss.Deserialize<CaptchaResponse>(responseData);

            return cResponse.success;
        }
        catch (WebException) {
            return false;
        }
    }

    class CaptchaResponse
    {
        public bool success { get; set; }
    }

当我们调用ValidateRecaptcha()方法时,它返回false。此外,如果我在onSubmit()函数上放置一个断点,它永远不会去那里,也不会显示Recaptcha图片。我已经确认这两个密钥对于我们注册的内容是正确的。我确实得到了#C;受到了reCAPTCHA的保护&#34;网页右下方的消息。我在References中引用了Recaptcha.dll。我希望有一些我们可以忽略的明显和简单的东西。我们目前正在使用本网站上的旧版1.0 ReCaptcha。

1 个答案:

答案 0 :(得分:0)

查看我自己的一些WebForms重新访问代码,看起来您发送的错误请求变量Request["grecaptcha-execute"]作为响应而不是Request["g-recaptcha-response"]

我也不确定将秘密作为表单数据附加是正确的方法(尽管它可能非常有效),当我看到的实现将把请求的url设置为{{1}时}

这是一个更完整的例子:

https://www.google.com/recaptcha/api/siteverify?secret={captcha_secret_key}&response={Request["g-recaptcha-response"]}&remoteip={Request.UserHostAddress}