This is a migrated thread and some comments may be shown as answers.

Captcha validation after invalid login attempt

1 Answer 518 Views
Captcha
This is a migrated thread and some comments may be shown as answers.
Shahi
Top achievements
Rank 1
Shahi asked on 09 Apr 2013, 02:50 AM
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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 09 Apr 2013, 06:38 AM
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.
Tags
Captcha
Asked by
Shahi
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or