Google Recaptcha不可见ASP.NET

时间:2016-09-28 08:48:40

标签: c# asp.net recaptcha

我正在尝试在ASP.NET网络表单上设置Google reCaptcha,但它不可见。

到目前为止,我采取了以下方法:

在.ascx页面的开头:

<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

以下表格我希望它显示:

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

<div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>

<recaptcha:RecaptchaControl
    ID="recaptcha"
    runat="server"           
    PublicKey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"
    PrivateKey="_My private key taken from the reCaptcha API_"            
    />

但是,当我运行页面时,reCaptcha不可见。

感谢任何帮助。感谢

2 个答案:

答案 0 :(得分:1)

看起来你正在使用旧的reCaptcha。

Google弃用它,并鼓励您切换到NoCaptcha re Captcha 2.

以下是您的工作方式:

  

page.aspx

<html>
    <head>
        <script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
    <body>
        <form>

                          

  

page.aspx.cs

public partial class page : System.Web.UI.Page
{
    private string CAPTCHA_SECRET_KEY = @"6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"; // WARNING: FAKE SECRET KEY
    protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
        {
            String username = unameInput.Text;
            String password = pwordInput.Text;

            if (validate())
            {
                // ...
            }
        }
    }

    // Thanks to http://www.thatsoftwaredude.com/content/6235/implementing-googles-no-captcha-recaptcha-in-aspnet
    private bool validate()
    {
        string url = @"https://www.google.com/recaptcha/api/siteverify";
        WebRequest request = WebRequest.Create(url);
        string postData = string.Format("secret={0}&response={1}&remoteip={2}", CAPTCHA_SECRET_KEY, Request["g-recaptcha-response"], Request.UserHostAddress);

        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 true; // TODO: Change to false when releasing
    }
}
class CaptchaResponse
{
    public bool success { get; set; }
}

答案 1 :(得分:0)

使用它。创建一个外部类

public class Recaptcha
{
    public bool ValidateCaptcha(string sitekey, string responseRecaptcha)
    {
        //var response = Request["g-recaptcha-response"];  //part of the parameter << you need to pass this as your responseRecaptcha
        //secret that was generated in key value pair
       //part of the parameter

        var client = new WebClient();
        var reply =
            client.DownloadString(
                string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}",sitekey,responseRecaptcha));

        var captchaResponse = JsonConvert.DeserializeObject<CaptchaResponse>(reply);
        string status = "";
        //when response is false check for the error message
        if (!captchaResponse.Success)
        {
            if (captchaResponse.ErrorCodes.Count <= 0) return true;

            var error = captchaResponse.ErrorCodes[0].ToLower();
            switch (error)
            {
                case ("missing-input-secret"):
                    status = "The secret parameter is missing.";
                    return false;
                    break;

                case ("invalid-input-secret"):
                    status = "The secret parameter is invalid or malformed.";
                    return false;
                    break;

                case ("missing-input-response"):
                    status = "The response parameter is missing.";
                    return false;
                    break;

                case ("invalid-input-response"):
                    status = "The response parameter is invalid or malformed.";
                    return false;
                    break;

                default:
                    status = "Error occured. Please try again";
                    return false;
                    break;
            }
        }
        else
        {
            status = "Valid";
        }


        return true;
    }
}

internal class CaptchaResponse
{
    [JsonProperty("success")]
    public bool Success { get; set; }

    [JsonProperty("error-codes")]
    public List<string> ErrorCodes { get; set; }
}