How To Block The UserName After 3 Invalid Password Attempts
In this post we will learn How To Block The UserName After 3 Invalid Password Attempts.Earlier we have discussed How To Create Login Form In Asp.Net and How To Create Login Form – CSS3.
Here after three invalid login attempts we will show user that your account has been locked, contact system administrator for more information, to accomplish this we are using viewstate.
Design View:
01 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
07 | < link href = "Style.css" rel = "stylesheet" /> |
08 | < title >How To Create Login Form In Asp.Net</ title > |
11 | < form id = "form1" runat = "server" > |
13 | < h1 style = "text-align:center" >How To Create Login Form In Asp.Net</ h1 > |
16 | < p >< asp:TextBox ID = "uname" runat = "server" type = "text" placeholder = "Username" ></ asp:TextBox ></ p > |
17 | < p >< asp:TextBox ID = "upass" runat = "server" type = "password" placeholder = "Password" ></ asp:TextBox ></ p > |
20 | < a href = "#" >Forgot Password ?</ a > |
24 | < asp:Button runat = "server" ID = "submir" type = "submit" Text = "Login" OnClick = "submir_Click" /> |
28 | < p >© 2013 All rights reserved by HighTechnology.in |
Code View:
02 | using System.Collections.Generic; |
06 | using System.Web.UI.WebControls; |
07 | using System.Data.SqlClient; |
09 | using System.Configuration; |
10 | public partial class _Default : System.Web.UI.Page |
12 | protected void Page_Load( object sender, EventArgs e) |
16 | protected void submir_Click( object sender, EventArgs e) |
18 | SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[ "connnn" ].ConnectionString); |
20 | SqlCommand cmd = new SqlCommand( "select * from users where UserName =@username and Password=@password" , con); |
21 | cmd.Parameters.AddWithValue( "@username" , uname.Text); |
22 | cmd.Parameters.AddWithValue( "@password" , upass.Text); |
23 | SqlDataAdapter da = new SqlDataAdapter(cmd); |
24 | DataTable dt = new DataTable(); |
26 | if (dt.Rows.Count > 0) |
28 | Response.Redirect( "Home.aspx" ); |
32 | if (System.Convert.ToInt32(ViewState[ "Tries" ]) > 3) |
33 | ClientScript.RegisterStartupScript(Page.GetType(), "validation" , "<script language='javascript'>alert('User Blocked, Contact System Administrator For More information.')</script>" ); |
37 | ViewState[ "Tries" ] = System.Convert.ToInt32(ViewState[ "Tries" ]) + 1; |
38 | if (System.Convert.ToInt32(ViewState[ "Tries" ]) > 3) |
39 | ClientScript.RegisterStartupScript(Page.GetType(), "validation" , "<script language='javascript'>alert('User Blocked, Contact System Administrator For More information.')</script>" ); |
42 | ClientScript.RegisterStartupScript(Page.GetType(), "validation" , "<script language='javascript'>alert('Invalid Username and Password.')</script>" ); |
