Captcha validation after invalid login attempt

2 posts, 1 answers
  1. Shahi
    Shahi avatar
    17 posts
    Member since:
    Apr 2013

    Posted 08 Apr 2013 Link to this post

    Hi

    Is there any property available to display the RadCaptcha after two invalid login attempt? My requirement is that the user needs to undergo a captcha validation after entering an invalid username/password two times. Please help.

    Thank you,
    Shahi.
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Apr 2013 Link to this post

    Hi Shahi,

    As far as I know, the RadCaptcha does not have a predefined property to display it after some particular number of invalid login attempt. Still from code behind, you can use your own logic to achieve the same requirement. Please have a look into the sample code I tried which works fine at my end. Each invalid login count I am storing in a session variable and when the count reaches two, i am displaying a DIV that contains the RadCaptcha.

    ASPX:
    <telerik:RadTextBox ID="RadTextBox1" runat="server" Label="UserName : ">
    </telerik:RadTextBox>
    <asp:RequiredFieldValidator ID="F1" runat="server" ControlToValidate="RadTextBox1"
        ValidationGroup="V1" ForeColor="Red">*</asp:RequiredFieldValidator>
    <br />
    <br />
    <telerik:RadTextBox ID="RadTextBox2" runat="server" Label="Password : " TextMode="Password">
    </telerik:RadTextBox>
    <asp:RequiredFieldValidator ID="F2" runat="server" ControlToValidate="RadTextBox2"
        ValidationGroup="V1" ForeColor="Red">*</asp:RequiredFieldValidator>
    <br />
    <br />
    <div id="divcaptcha" runat="server" visible="false" style="height: auto">
        <telerik:RadCaptcha ID="RadCaptcha1" runat="server" ErrorMessage="Invalid Captcha"
            CaptchaImage-TextLength="5" CaptchaImage-TextColor="Red" IgnoreCase="true" EnableRefreshImage="true"
            CaptchaImage-RenderImageOnly="true" ValidatedTextBoxID="RadTextbox3" ValidationGroup="V1">
        </telerik:RadCaptcha>
        <telerik:RadTextBox ID="RadTextbox3" runat="server">
        </telerik:RadTextBox>
        <asp:RequiredFieldValidator ID="F3" runat="server" ControlToValidate="RadTextbox3"
            ValidationGroup="V1" ForeColor="Red">*</asp:RequiredFieldValidator>
    </div>
    <br />
    <br />
    <telerik:RadButton ID="RadButton1" runat="server" Text="Login" OnClick="RadButton1_Click"
        ValidationGroup="V1">
    </telerik:RadButton>

    C#:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["flag"] = 0;
        }
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        string username = RadTextBox1.Text.Replace("'", "//").Trim();
        string password = RadTextBox2.Text.Replace("'", "//").Trim();
        bool captchaValidate = true;
        captchaValidate = RadCaptcha1.IsValid;
        try
        {
            SqlConnection con = new SqlConnection();
            SqlCommand loginCmd = new SqlCommand("loginCheck", con);
            SqlDataReader tableReader;
            loginCmd.CommandType = CommandType.StoredProcedure;
            loginCmd.Parameters.Add(new SqlParameter("@username", username));
            loginCmd.Parameters.Add(new SqlParameter("@password", password));
            if (captchaValidate == true)
            {
                // Your Business logic
                tableReader = loginCmd.ExecuteReader();
                if (tableReader.HasRows)
                {
                    //Your Forms Authentication Code.
                    //Checking user Roles
                    //Checking Query Strings etc.
                }
                else
                {
                    // Display Captcha Block after two invalid login
                    // Display invalid user login attempt
                    int x = Convert.ToInt32(Session["flag"]);
                    x = x + 1;
                    if (x > 1)
                    {
                        divcaptcha.Visible = true;
                    }
                    Session["flag"] = x;
                }
            }
            else
            {
                //Invalid Captcha entered.
            }
        }
        catch (Exception ex)
        {
            Response.Write("Login attempt failed : " + ex.ToString());
        }
        finally
        {
            //Disconnect DB Connection
        }
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top