C# winfrom實現輸入次數錯誤超過3次 鎖定十五分鐘登入
阿新 • • 發佈:2019-02-08
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using System.Data.SqlClient;
namespace QFrom
{
public partial class FrmLogin : Skin_Mac
{
public FrmLogin()
{
InitializeComponent();
}
//使用者登入文字框敲回車鍵
private void userTex_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
PwdTex.Focus();
}
}
//使用者密碼文字框敲回車鍵
private void PwdTex_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
}
}
string strconn = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
//登入
private void LoginBtn_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(strconn))
{
using (SqlCommand com = new SqlCommand())
{
com.CommandText = "select * from Account where [email protected]";
com.Connection = con;
con.Open();
com.Parameters.Add(new SqlParameter("username", userTex.Text));
//com.Parameters.Add(new SqlParameter("password", textBox2.Text));
using (SqlDataReader read = com.ExecuteReader())
{
if (read.Read()) //如果reader.Read()的結果不為空, 則說明輸入的使用者名稱存在
{
string passwored = read.GetString(read.GetOrdinal("AccountPass"));//讀取到的密碼
int errortimes = read.GetInt32(read.GetOrdinal("ErrorTimes")); //讀取錯誤登陸次數
DateTime errortime = read.GetDateTime(read.GetOrdinal("attach_start"));//讀取到的錯誤時間
//int errortimecount = DateTime.Now.Subtract(errortime).Minutes;//Subtract 是以當前時間的分鐘數減去錯誤時間的分鐘
if (errortimes > 3)
//判斷錯誤次數是否大於3,則開始禁止登入
{
if (DateTime.Now.Subtract(errortime).Minutes > 15)//當前時間與Error時間超過15分鐘 執行清空登陸Error
{
this.Errorclear();
}
else
{
MessageBox.Show("sorry 你已經登入錯誤超過3次,請15分鐘後嘗試!");
}
}
else if (passwored.Trim() == PwdTex.Text)
{
//FrmMain main = new FrmMain();
//main.Show();
MessageBox.Show("登陸成功!");
this.Errorclear();
}
else
{
MessageBox.Show("密碼錯誤!");
this.ErrorUpda();
}
}
else
{
MessageBox.Show("使用者不存在!");
}
}
}
}
}
//對比上一次錯誤時間 上一次錯誤時間在15分之前,就清空時間和錯誤 個數
public void Errorclear()//登陸成功後清空登陸次數ErrorTimes
{
using (SqlConnection con = new SqlConnection(strconn))
{
using (SqlCommand com = new SqlCommand())
{
com.CommandText =
"update Account set attach_start=getdate(), ErrorTimes=0 where [email protected]";
com.Parameters.Add(new SqlParameter("username", userTex.Text));
com.Connection = con;
con.Open();
com.ExecuteNonQuery();
}
}
}
public void ErrorUpda()//登陸失敗把錯誤登陸次數ErrorTimes+1
{
using (SqlConnection con=new SqlConnection(strconn))
{
using (SqlCommand com=new SqlCommand())
{
com.CommandText = "update Account set attach_start=getdate(), ErrorTimes=ErrorTimes+1 where AccountID='" +
userTex.Text.Trim() + "'";
com.Connection = con;
con.Open();
com.ExecuteNonQuery();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using System.Data.SqlClient;
namespace QFrom
{
public partial class FrmLogin : Skin_Mac
{
public FrmLogin()
{
InitializeComponent();
}
//使用者登入文字框敲回車鍵
private void userTex_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
PwdTex.Focus();
}
}
//使用者密碼文字框敲回車鍵
private void PwdTex_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
}
}
string strconn = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
//登入
private void LoginBtn_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(strconn))
{
using (SqlCommand com = new SqlCommand())
{
com.CommandText = "select * from Account where
com.Connection = con;
con.Open();
com.Parameters.Add(new SqlParameter("username", userTex.Text));
//com.Parameters.Add(new SqlParameter("password", textBox2.Text));
using (SqlDataReader read = com.ExecuteReader())
{
if (read.Read()) //如果reader.Read()的結果不為空, 則說明輸入的使用者名稱存在
{
string passwored = read.GetString(read.GetOrdinal("AccountPass"));//讀取到的密碼
int errortimes = read.GetInt32(read.GetOrdinal("ErrorTimes")); //讀取錯誤登陸次數
DateTime errortime = read.GetDateTime(read.GetOrdinal("attach_start"));//讀取到的錯誤時間
//int errortimecount = DateTime.Now.Subtract(errortime).Minutes;//Subtract 是以當前時間的分鐘數減去錯誤時間的分鐘
if (errortimes > 3)
//判斷錯誤次數是否大於3,則開始禁止登入
{
if (DateTime.Now.Subtract(errortime).Minutes > 15)//當前時間與Error時間超過15分鐘 執行清空登陸Error
{
this.Errorclear();
}
else
{
MessageBox.Show("sorry 你已經登入錯誤超過3次,請15分鐘後嘗試!");
}
}
else if (passwored.Trim() == PwdTex.Text)
{
//FrmMain main = new FrmMain();
//main.Show();
MessageBox.Show("登陸成功!");
this.Errorclear();
}
else
{
MessageBox.Show("密碼錯誤!");
this.ErrorUpda();
}
}
else
{
MessageBox.Show("使用者不存在!");
}
}
}
}
}
//對比上一次錯誤時間 上一次錯誤時間在15分之前,就清空時間和錯誤 個數
public void Errorclear()//登陸成功後清空登陸次數ErrorTimes
{
using (SqlConnection con = new SqlConnection(strconn))
{
using (SqlCommand com = new SqlCommand())
{
com.CommandText =
"update Account set attach_start=getdate(), ErrorTimes=0 where
com.Parameters.Add(new SqlParameter("username", userTex.Text));
com.Connection = con;
con.Open();
com.ExecuteNonQuery();
}
}
}
public void ErrorUpda()//登陸失敗把錯誤登陸次數ErrorTimes+1
{
using (SqlConnection con=new SqlConnection(strconn))
{
using (SqlCommand com=new SqlCommand())
{
com.CommandText = "update Account set attach_start=getdate(), ErrorTimes=ErrorTimes+1 where AccountID='" +
userTex.Text.Trim() + "'";
com.Connection = con;
con.Open();
com.ExecuteNonQuery();
}
}
}