输入错了三次禁止登录, 15分钟后才能继续. 用数据库记录ErrorTimes, 最后出错时间uLoginTime数据导入
第一步:
在App.config中添加连接字符串
cs
<connectionStrings>
<add name="conStr" connectionString="Data Source = WINGEL; Initial Catalog = People; Trusted_Connection = SSPI"/>
</connectionStrings>
创建一个在项目中的文件夹, 取名叫service, 然后创建一个接口类叫userService.cs
cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dome16_三次锁定.service
{
internal interface userService
{
// 判断用户是否被锁定
bool IsLock(string name);
// 更新错误次数归0
void UpdateErrorTime(string name);
// 登入是否成功
bool IsLoginSuccess(string name, string pwd);
// 是否注册成功
bool IsRegister(string name, string pwd);
// 更新时间和错误登入次数
void UpdateLoginTime_ErrorTime(string name);
}
}
创建一个文件夹在service文件夹内, 取名叫serviceimpl, 然后再创建一个实现接口的类叫userServiceimpl.cs
cs
using Dome16_三次锁定.utils;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dome16_三次锁定.service.serviceimpl
{
internal class userServiceimpl : userService
{
string conStr { get; set; }
public userServiceimpl()
{
this.conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
}
// 判断用户是否被锁定
public bool IsLock(string name)
{
bool b = false;
using (SqlConnection conn = new SqlConnection(conStr))
{
string sql = "select * from [User] where uErrorTime = 3 and " +
"Datediff(minute, uLoginTime, getdate()) < 15 and uLoginName = @name";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@name", name);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
b = true;
}
}
}
return b;
}
// 更新错误次数归0
public void UpdateErrorTime(string name)
{
using (SqlConnection conn = new SqlConnection(conStr))
{
string sql = "update [User] set uErrorTime = 0 where uLoginName = @name";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@name", name);
conn.Open();
cmd.ExecuteNonQuery(); // 增删改
}
}
}
// 登入是否成功
public bool IsLoginSuccess(string name, string pwd)
{
bool b = false;
using (SqlConnection conn = new SqlConnection(conStr))
{
string sql = "select * from [User] where uLoginName = @name and uPwd = @pwd";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
pwd = Md5Util.Md5Create(pwd);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@pwd", pwd);
conn.Open();
SqlDataReader r = cmd.ExecuteReader();
if (r.HasRows)
{
b = true;
}
}
}
return b;
}
// 是否注册成功
public bool IsRegister(string name, string pwd)
{
bool b = false;
using (SqlConnection conn = new SqlConnection(conStr))
{
string sql = "select * from [User] where uLoginName = @name";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@name", name);
conn.Open ();
SqlDataReader reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
if(Register(name, pwd))
{
b = true;
}
}
}
}
return b;
}
bool Register(string name, string pwd)
{
bool b = false;
using (SqlConnection conn = new SqlConnection(conStr))
{
string sql = "INSERT into [User] VALUES (@name,@pwd,@logintime,@errortime)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
pwd = Md5Util.Md5Create(pwd);
cmd.Parameters.AddWithValue ("@name", name);
cmd.Parameters.AddWithValue("@pwd", pwd);
cmd.Parameters.AddWithValue("@logintime", DateTime.Now);
cmd.Parameters.AddWithValue("@errortime", 0);
conn.Open ();
int r = cmd.ExecuteNonQuery();
if(r > 0)
{
b = true;
}
}
}
return b;
}
// 更新时间和错误登入次数
public void UpdateLoginTime_ErrorTime(string name)
{
using (SqlConnection conn = new SqlConnection(conStr))
{
string sql = "UPDATE [User] SET uErrorTime = uErrorTime + 1, uLoginTime = GETDATE() where uLoginName = @name";
using(SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@name", name);
conn.Open ();
cmd.ExecuteNonQuery();
}
}
}
}
}