密码错误3次,30分钟内无法登录源码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Configuration;

using System.Data.SqlClient;

 

namespace 用户登录

{

    public partial class Form1 : Form

    {

        publicForm1()

        {

            InitializeComponent();

        }

 

        ///<summary>

        ///增加错误次数的方法

        ///</summary>

        privatevoid IncErrorTime()

        {

            stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            using(SqlConnection conn = new SqlConnection(ConnStr))

            {

                using(SqlCommand updateCmd =conn.CreateCommand())

                {

                    updateCmd.CommandText = "update T_Admin set errortime=errortime+1 whereUsername=@username";

                    updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));

                    conn.Open();

                    updateCmd.ExecuteNonQuery();

                }           

            } 

        }

 

        ///<summary>

        ///错误次数清0

        ///</summary>

        privatevoid NotErrorTime()

        {

            stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            using(SqlConnection conn = new SqlConnection(ConnStr))

            {

                using(SqlCommand updateCmd =conn.CreateCommand())

                {

                    updateCmd.CommandText = "update T_Admin set errortime=0 whereUsername=@username";

                    updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));

                    conn.Open();

                   updateCmd.ExecuteNonQuery();

                }

            }

        }

 

        ///<summary>

        ///密码错误3,记录当前时间加30分钟

        ///</summary>

        privatevoid IncLoginTime()

        {

            stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            using(SqlConnection conn = new SqlConnection(ConnStr))

            {

                using(SqlCommand updateCmd =conn.CreateCommand())

                {

                    updateCmd.CommandText = "update T_Admin set logintime=@logintime whereUsername=@username";

                    DateTimelogintime = DateTime.Now.AddMinutes(30);

                    updateCmd.Parameters.Add(new SqlParameter("username", txtUserName.Text));

                    updateCmd.Parameters.Add(new SqlParameter("logintime", logintime.ToString()));

                    conn.Open();

                   updateCmd.ExecuteNonQuery();

                }

            }

        }

 

        privatevoid button1_Click(objectsender, EventArgs e)

        {

            stringusername = txtUserName.Text;

            stringpassword = txtPassword.Text;                       

            stringConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;

            using(SqlConnection conn = new SqlConnection(ConnStr))

            {

                using(SqlCommand cmd = conn.CreateCommand())

                {

                    cmd.CommandText = "select * from T_Admin whereusername=@username";

                    cmd.Parameters.Add(new SqlParameter("username", username));                  

                    conn.Open();

                    using(SqlDataReader reader = cmd.ExecuteReader())

                    {

                        if (reader.Read())

                        {

                            //用户存在

                            string dbpassword =reader.GetString(reader.GetOrdinal("password"));

                            DateTime logintime =reader.GetDateTime(reader.GetOrdinal("logintime"));

                            //判断当前时间是是服务器允许登录时间

                            if (logintime > DateTime.Now)

                            {

                                MessageBox.Show("一定时间内禁止登录");

                                return;

                            }

                            //如果密码正确

                            if (dbpassword == txtPassword.Text)

                            {

                               

                                NotErrorTime();

                                MessageBox.Show("登录成功!");

                            }

                            //如果密码错误

                            else

                            {

                                int errortime = reader.GetInt32(reader.GetOrdinal("errortime"));

                                if (errortime >= 2)

                                {

                                    MessageBox.Show("密码错误次数太多!");

                                   IncLoginTime();

                                   NotErrorTime();

                                    return;

                                }                               

                                MessageBox.Show("密码错误!");

                                IncErrorTime();//密码错误,次数加1

                            }

                        }

                        else//用户名不存在

                        {                           

                            MessageBox.Show("用户名不存在!");

                            return;

                        }

                    }                   

                }

            }

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值