i'm trying to implement viewstate for user login failed attempt set the limit is 3 after 3 try user will not be allowed to login for 30 mins. i have write the code but viewstate is not working.
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-GG8IP5K\SQLEXPRESS;Initial Catalog =Airtel;integrated security=true");
//string strConstr = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
//SqlConnection con = new SqlConnection(strConstr);
int Count = 0;
protected void Page_Load(object sender, EventArgs e)
{
((Airtel.Site1)(this.Master)).lbl_Time.Visible = false;
if (!IsPostBack)
{
if (Request.Cookies["custid"] != null)
userid.Text = Request.Cookies["custid"].Value;
if (Request.Cookies["Pswd"] != null)
txt_Pswrd.Attributes.Add("value", Request.Cookies["Pswd"].Value);
if (Request.Cookies["custid"] != null && Request.Cookies["Pswd"] != null)
chckboxLogin.Checked = true;
Session["login_time"] = DateTime.Now.ToString();
Session["UserId"] = userid.Text;
Session["SqlCon"] = con;
con.Open();
}
}
protected void userid_TextChanged(object sender, EventArgs e)
{
}
private string Decrypt(string clearText)
{
string EncryptionKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
protected void btn_Login_Click(object sender, EventArgs e)
{
//SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-GG8IP5K\SQLEXPRESS;Initial Catalog =Airtel;integrated security=true");
Session["SqlCon"] = con;
con.Open();
//if (ViewState["Count"] != null)
//{
// lblCount.Text = (int.Parse(lblCount.Text) + 1).ToString();
//}
if (ViewState["Count"] != null)
{
Count = (int)ViewState["Count"] + 1;
}
lblCount.Text = Count.ToString();
ViewState["Count"] = Count;
if (Count > 3)
{
lblCount.Text = "You Exceed the limit please try again later";
}
//txtLoginAttmpt.Text = Count.ToString();
SqlCommand cmd = new SqlCommand("splogin", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("custid", userid.Text);
SqlParameter p2 = new SqlParameter("Pswd", Decrypt(txt_Pswrd.Text));
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
Response.Write("<script>alert('Login Successfully...!')</script>");
Session["UserId"] = userid.Text;
if (chckboxLogin.Checked == true)
{
Response.Cookies["custid"].Value = userid.Text;
Response.Cookies["Pswd"].Value = txt_Pswrd.Text;
Response.Cookies["custid"].Expires = DateTime.Now.AddYears(10);
Response.Cookies["Pswd"].Expires = DateTime.Now.AddYears(10);
Response.Redirect("Call.aspx");
}
else
{
Response.Cookies["userid"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(-1);
Response.Redirect("Call.aspx");
}
}
//if (rd.HasRows)
//{
// rd.Read();
// Response.Write("<script>alert('Login Successfully...!')</script>");
// Session["UserId"] = userid.Text;
// Response.Redirect("Call.aspx");
//}
else
{
Response.Write("<script>alert('Invalid username or password....!')</script>");
Response.Write("<script> window.location ='Loginpage.aspx'</script>");
}
con.Close();
}
}