Friday, 29 August 2014

Recapcha Implementaions

Step1 : Download the Recapcah DLL From

https://code.google.com/p/recaptcha/downloads/list?q=label:aspnetlib-Latest

Add Reference to your solution..

Register assembly on your page

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

Add Basic Code

 <div>
            <asp:TextBox runat="server" ID="txtname" ValidationGroup="myGroup" />


            <asp:RequiredFieldValidator ID="rfvtxtname" ValidationGroup="myGroup" ControlToValidate="txtname" ErrorMessage="enter data"
                Display="Dynamic" runat="server" />

            <recaptcha:RecaptchaControl
                ID="recaptcha"
                runat="server"
                PublicKey="your key"
                PrivateKey="your key" />


            <asp:Button runat="server" ID="clickMe" Text="Click Me" ValidationGroup="myGroup" OnClick="clickMe_Click" />

            <asp:Label Visible="False" ID="lblResult" runat="server" />

        </div>


The code behind changes are like this


      const string CHALLENGE_FIELD_KEY = "recaptcha_challenge_field";
        const string RESPONSE_FIELD_KEY = "recaptcha_response_field";

        // Getting Proxy for Recaptcha
        if (ConfigurationManager.AppSettings["webProxy"] != null)
        {
            string proxyAdress = ConfigurationManager.AppSettings["webProxy"].ToString();
            if (!string.IsNullOrEmpty(proxyAdress))
            {
                WebProxy webproxy = new WebProxy(proxyAdress);
                recaptcha.Proxy = webproxy;
            }
        }


        var captchaChallengeValue = HttpContext.Current.Request.Form[CHALLENGE_FIELD_KEY];
        var captchaResponseValue = HttpContext.Current.Request.Form[RESPONSE_FIELD_KEY];

        var captchaValidtor = new Recaptcha.RecaptchaValidator
                                  {
                                      PrivateKey = "mykey",
                                      RemoteIP = HttpContext.Current.Request.UserHostAddress,
                                      Challenge = captchaChallengeValue,
                                      Response = captchaResponseValue
                                  };

        var recaptchaResponse = captchaValidtor.Validate();


        var IsValid = recaptchaResponse.IsValid;
        if (!IsValid)
        {
            lblResult.Text = recaptchaResponse.ErrorMessage;
        }

        Page.Validate("myGroup");

        if (Page.IsValid && IsValid)
        {
            lblResult.Text = "You Got It!";
            lblResult.ForeColor = System.Drawing.Color.Green;
        }
        else
        {    
            lblResult.ForeColor = System.Drawing.Color.Red;
        }







No comments:

Post a Comment

Featured post

Getting Started With SOLR

Getting Started with Solr ·           Features o    Hit highlighting o    Auto correction o    Faceted Search o    Multi lan...