如何在使用sql server 2008的c#windows窗体中3次登录表单尝试失败后限制用户 [英] How to restrict a user after 3 unsuccessful attempts in login form in c# windows forms with sql server 2008

查看:93
本文介绍了如何在使用sql server 2008的c#windows窗体中3次登录表单尝试失败后限制用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的名字是vishal。我有一个名为: Project 的应用程序,其mdi父窗体名为: MDIParent1 。我有一个名为: frmLogin 的登录表单。下面是我使用sql server 2008的frmLogin的c#代码:

Hi my name is vishal.I have a application named:Project which has mdi parent form named:MDIParent1. I have a login form named:frmLogin. Given below is my c# code of frmLogin with sql server 2008.:

using System.Data.SqlClient;
namespace Project
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
private void btnLogin_Click(object sender, EventArgs e)
        {
            if ((txtPassword.Text == "tuscano") && (txtUsername.Text.ToLower() == "kumar"))
            {
                MDIParent1 g = new MDIParent1();
                g.Show();
                this.Close();
            }
            else
            {
                string username = txtUsername.Text;
                string password = txtPassword.Text;
                bool validUser = ValidateUser(username, password);
                if (validUser)
                {
                    MDIParent1 m = new MDIParent1();
                    m.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Invalid user name or password. Please try with another user name or password", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtUsername.Focus();
                }
            }
        }
private bool ValidateUser(string username, string password)
        {
            bool success = false;
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd = new SqlCommand("Select @count=Count(*) from [dbo].[User] where username=@username and password=@password", conn);
            cmd.Parameters.AddWithValue("@username", txtUsername.Text);
            cmd.Parameters.AddWithValue("@password", txtPassword.Text);
            cmd.Parameters.Add("@count", SqlDbType.Int).Direction = ParameterDirection.Output;
            conn.Open();
            cmd.ExecuteNonQuery();
            if (Convert.ToInt32(cmd.Parameters["@count"].Value) > 0)
            {
                success = true;
            }
            else
            {
                success = false;
            }
            conn.Close();
            return success;
        }



上面的c#代码完全没问题!

但是想要的是在 3次尝试失败(登录/尝试失败),向用户显示一个消息框告诉抱歉,您尝试使用错误的用户名或错误的密码访问应用程序!抱歉,麻烦!。谁能帮助我/指导我如何达到这个必要的结果?!谁能帮帮我吧!任何帮助/指导解决这个问题将不胜感激!


The above c# code Works with no problem at all!
However what is want is to restrict a user after 3 unsuccessful attempts(failed logins/attempts) by displaying a message box to user telling "Sorry you have tried to access application either with wrong user name or wrong password! Sorry for the trouble!". Can anyone help me/guide me on how to achieve this required result?! Can anyone help me Please! Any help/guidance in solving of this problem would be greatly appreciated!

推荐答案

应该很简单:添加 failure_count (或其他)字段到用户表:在每次失败时递增它,并在每次成功登录时将其重置为0。然后
It should be simple: add a failure_count (or whatever) field to the user table: increment it at each failure and reset it to 0 on each successfull login. Then
Select @count=Count(*) from [dbo].[User] where username=@username and password=@password and failure_count < 3"

应该可以做到这一点。


我认为你必须使用btnLogin_Click以上的全局静态int变量,例如,



private int static hitcount = 0;

if(hitcount!= 3)

{

if(validUser)

{

MDIParent1 m = new MDIParent1();

m.Show();

this.Close() ;

}

其他

{

MessageBox.Show(无效的用户名或密码。请尝试使用另一个用户名或密码,DRRS,MessageBoxButtons.OK,MessageBoxIcon.Warning);

txtUsername.Focus();}

hitcount ++;}

}

其他

{

MessageBox.Show(抱歉,您尝试使用错误的用户名访问应用程序或密码错误!对不起!#,DRRS,MessageBoxButtons.OK, MessageBoxIcon.Warning);

txtUsername.Focus();

}







在else块下你还可以将hitcount保存在数据库中,这样如果用户再次尝试登录,那么通过代表用户名和密码检查hitcount,我们可以限制用户登录。
I think you have to use Global Static int variable above btnLogin_Click e.g.,

private int static hitcount=0;
if(hitcount !=3)
{
if (validUser)
{
MDIParent1 m = new MDIParent1();
m.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid user name or password. Please try with another user name or password", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUsername.Focus();}
hitcount ++; }
}
else
{
MessageBox.Show("Sorry you have tried to access application either with wrong user name or wrong password! Sorry for the trouble!", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUsername.Focus();
}



Under "else" block you can also save the "hitcount" in database so that if user again try to login then again by checking the hitcount on behalf of username and password we can restrict the user to login.


这篇关于如何在使用sql server 2008的c#windows窗体中3次登录表单尝试失败后限制用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆