Google RECaptcha with URL routing in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

Hi,

I have successfully implement Google RECaptcha Code in my web application by referring below link. http://www.aspsnippets.com/Articles/Google-RECaptcha-Code-with-Example-in-ASPNet.aspx But i found that it's not working if i've used URL Rewriter. For an instance: www.abcde.com/webpages/index.aspx (working) javascript:                                                

url: "index.aspx/VerifyCaptcha",  www.abcde.com/home (not working) javascript:                                                

url: "home/VerifyCaptcha", 
url: "/home/VerifyCaptcha", 

 

You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago Modified on 3 months ago

Hi counterkin,

Here i have created a sample with url routing.

Global.asax

<script runat="server">
    void Application_Start(object sender, EventArgs e) 
    {
        RegisterRoutes(RouteTable.Routes);
    }
    
    static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Customers", "Customers", "~/Customers.aspx");
        routes.MapPageRoute("CustomerDetails", "Customers/{CustomerId}", "~/CustomerDetails.aspx");
    }
</script>

HTML

Default.aspx

<h1>
    Home</h1>
<hr />
<a href='<%=ResolveUrl("~/Customers") %>'>View Customers</a></form>

Customers.aspx

<h1>
    Customers</h1>
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
        <asp:HyperLinkField Text="View" DataNavigateUrlFormatString="~/Customers/{0}" DataNavigateUrlFields="Id" />
    </Columns>
</asp:GridView>
<br />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
    async defer></script>
<script type="text/javascript">
    var onloadCallback = function () {
        grecaptcha.render('dvCaptcha', {
            'sitekey': '<%=ReCaptcha_Key %>',
            'callback': function (response) {
                $.ajax({
                    type: "POST",
                    url: "Customers.aspx/VerifyCaptcha",
                    data: "{response: '" + response + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (r) {
                        var captchaResponse = jQuery.parseJSON(r.d);
                        if (captchaResponse.success) {
                            $("[id*=txtCaptcha]").val(captchaResponse.success);
                            $("[id*=rfvCaptcha]").hide();
                        } else {
                            $("[id*=txtCaptcha]").val("");
                            $("[id*=rfvCaptcha]").show();
                            var error = captchaResponse["error-codes"][0];
                            $("[id*=rfvCaptcha]").html("RECaptcha error. " + error);
                        }
                    }
                });
            }
        });
    };
</script>
<div id="dvCaptcha">
</div>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />

CustomerDetails.aspx

<h1>
    Customer Details</h1>
<hr />
<b>Customer Id:</b>
<asp:Label ID="lblCustomerId" runat="server" />

Code

Customers.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

protected static string ReCaptcha_Key = "key";
protected static string ReCaptcha_Secret = "secret";

[WebMethod]
public static string VerifyCaptcha(string response)
{
    string url = "https://www.google.com/recaptcha/api/siteverify?secret=" + ReCaptcha_Secret + "&response=" + response;
    return (new WebClient()).DownloadString(url);
}

CustomerDetails.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        lblCustomerId.Text = this.Page.RouteData.Values["CustomerId"].ToString();
    }
}

Screenshot

I agree, here is the link: http://e-iceblue.com/free-api