locked
[Question] ASP.NET WebForm C# + Google reCAPTCHA v3 RRS feed

  • Question

  • User-60902076 posted

    Fail ! Why ? How to fix ? 

    Thank you very much.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication8.Default" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=6Ld1XXXXX(略刪)"></script>
    <script type="text/javascript">
    grecaptcha.ready(function () {
    grecaptcha.execute('6Ld1XXXXX(略刪)', { action: 'login' }).then(function (token) {
    document.getElementById('returnToken').value = token;
    document.getElementById('abc').value = 456;
    });
    });
    </script>
    </head>
    <body>
    <form id="form1" runat="server" method="post">
    <div>
    <input type="hidden" id="returnToken" name="returnToken" />
    <input type="hidden" id="abc" name="abc" value="123" />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
    <asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
    </body>
    </html>

    Default.aspx.cs

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Script.Serialization;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using static WebApplication8.Default;

    namespace WebApplication8
    {

    public partial class Default : System.Web.UI.Page
    {

    public class ResponseToken
    {
    public bool success { get; set; }
    public float score { get; set; }
    public string action { get; set; }
    public DateTime challenge_ts { get; set; }
    public string hostname { get; set; }
    public List<string> ErrorCodes { get; set; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    if (GoogleReCaptchaV3Validate())
    {
    lblmsg.Text = "Valid Recaptcha";
    lblmsg.ForeColor = System.Drawing.Color.Green;
    }

    else
    {
    lblmsg.Text = "Not Valid Recaptcha";
    lblmsg.ForeColor = System.Drawing.Color.Red;
    }
    }
    public bool GoogleReCaptchaV3Validate()
    {

    string Response = Request["g-recaptcha-response"];//Getting Response String Append to Post Method
    string recaptcha = Request.Form["returnToken"].ToString();
    string aaa = Request.Form["abc"];
    //if (recaptcha == null || recaptcha == "")
    //{
    // return false;
    //}
    string securetKey = "6Ld1XXXXX(略刪)";
    bool Valid = false;
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create
    ("https://www.google.com/recaptcha/api/siteverify?secret=6Ld1XXXXX(略刪)&response=" + recaptcha);
    try
    {
    //Google recaptcha Response
    using (WebResponse wResponse = req.GetResponse())
    {

    using (StreamReader readStream = new StreamReader(wResponse.GetResponseStream()))
    {
    // jsonResponse always "", why ?
    string jsonResponse = readStream.ReadToEnd();

    JavaScriptSerializer js = new JavaScriptSerializer();
    //MyObject data = js.Deserialize<MyObject>(jsonResponse);// Deserialize Json
    //JObject jObject = js.Deserialize<JObject>(jsonResponse);// Deserialize Json

    // Rootobject object = JsonConvert.DeserializeObject<Rootobject>(jsonString);
    //JObject jObject = JsonConvert.DeserializeObject<JObject>(jsonResponse);

    ResponseToken response = new ResponseToken();
    //response = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponseToken>(jsonResponse);
    ResponseToken jObject = JsonConvert.DeserializeObject<ResponseToken>(jsonResponse);

    //bool s = jObject.success;
    //Valid = Convert.ToBoolean(jObject.success);
    }
    }

    return Valid;
    }
    catch (WebException ex)
    {
    throw ex;
    }
    }
    }
    }

    Tuesday, April 16, 2019 10:04 AM

Answers

  • User36583972 posted


    Hi luicst,

    Fail ! Why ? How to fix ? 

    I suggest you can describe the issue with a detailed description about the results including any exception messages.

    Besides, if you have any question about reCAPTCHA , you can go to the reCAPTCHA support center for getting suitable help.


    Best Regards

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 17, 2019 9:16 AM