简体   繁体   English

如果用户多次输入无效的用户名和密码,如何终止程序

[英]how to Terminate program if user inputs invalid username and password several times

The problem in this code is that it doesn't terminate the program even though I clicked the login button with a invalid username and password 3 times. 此代码中的问题是,即使我用无效的用户名和密码单击登录按钮3次,它也不会终止程序。 So what I want to happen is that when the user inputs a invalid username and password 3 times, it will terminate the program. 所以我想发生的是,当用户输入无效的用户名和密码3次时,它将终止程序。

MessageBox.Show("Invalid username and/or password.");
pass_txt.Text = null;

int login_count = 0;
login_count = login_count + 1;
if (login_count == 3)
{
    Application.Exit();
}
int login_count = 0;
login_count = login_count + 1;

Login_count will never be greater than 1, since you initialize it to 0 right before you increment it. Login_count永远不会大于1,因为您在将其递增之前将其初始化为0。 You need to make it global, then increment it without setting it back to 0. 您需要将其设置为全局,然后递增而不将其设置回0。

You'll need something closer to 您将需要一些更接近的东西

if(failedLogin)
{
    // ...
    this.login_count++;
}

// ...
if (this.login_count == 3)
{
    Application.Exit();
}

Something like this might work. 这样的事情可能会起作用。

    private void Login()
    {
        int login_count = 0;
        bool goodLogin = false;
        do
        {
            if (DoLogin())
                goodLogin = true;
            else
                login_count++;

        } while (login_count < 4 && !goodLogin);

        if (goodLogin)
        {
            //do the login stuff
        }
        else
        {
            Application.Exit();
        }
    }

    private bool DoLogin()
    {
        if (true) //do the login logic here
            return true;
        else
        {
            MessageBox.Show("Invalid username and/or password.");
            pass_txt.Text = null;
            return false;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以计算用户使用错误密码或用户名登录的次数 - Is it possible to count how many times a user has logged with wrong password or username “用户名/密码对无效” - “username/password couple is invalid” 无效的用户名/密码 - invalid username/password C#使用SQL的简单用户名/密码,用户始终无效 - C# simple username/password using SQL, user always invalid 如何将带有错误说明(无效的用户名或无效的密码)的HttpResponseException返回到前端? - How to return HttpResponseException with error specificaton (invalid username or invalid password) to the frontend? 用户输入无效凭据三次后如何显示密码恢复表单 - How to show password recovery form after user enters invalid credentials three times 如何获取当前用户的用户名,密码和域 - How to get username, password and domain of current user 如何通过用户名和密码注册用户并通过会员身份确认密码和电子邮件? - How to register user by username and password and confirm password and email by membership? 无效的用户名或密码 eroor 页面 - Invalid username or password eroor page 在ASP.NET中尝试3次无效密码后如何阻止用户名 - How To Block The UserName After 3 Invalid Password Attempts IN ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM