第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用戶嘗試使用計數(shù)

用戶嘗試使用計數(shù)

C#
森林海 2021-11-14 10:17:05
我想使用條件在我的計數(shù)中創(chuàng)建用戶嘗試。我如何使用條件來執(zhí)行這種邏輯以及在下面的代碼中放置的位置。示例:如果用戶嘗試登錄 3 次,但用戶輸入錯誤并通過,他將收到一條消息,提示您已達(dá)到登錄嘗試次數(shù)private void btn_login_Click(object sender, EventArgs e){        var obj = new Usercontrols.SIMSMain();        obj.Dock = DockStyle.Fill;        conn.Open();        SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab",conn);        selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);        selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);        SqlDataReader dataReader;           dataReader = selectCommand.ExecuteReader();        var count = 0;        while (dataReader.Read())        {            count = count + 1;        }    if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))    {        MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    }    else    {        if (count == 1)        {            MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);            this.Hide();            this.Parent.Controls.Add(obj);        }        else if (count == 3)        {            count++;            MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);        }        else        {            MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);        }        }    conn.Close();}
查看完整描述

3 回答

?
慕哥9229398

TA貢獻(xiàn)1877條經(jīng)驗 獲得超6個贊

這是您修改后的代碼,用于處理超過 3 次的嘗試


Timer loginAttempsTimeOut;

Dictionary<string, int> loginAttempsPerUser = new Dictionary<string,int>();

Dictionary<string, DateTime> loginAttemptsViolated = new Dictionary<string, DateTime>();

int TimeOutInMinutes = 15;


private void SetTimer()

{

    loginAttempsTimeOut = new Timer();

    loginAttempsTimeOut.Interval = 1000 * 60; // check timeout every 1 mins

    loginAttempsTimeOut.Enalbed = true;

    loginAttempsTimeOut.Tick += LoginAttempsTimeOut_Tick;

}


// set a timer, and if login timeout for each user is elapsed,

// allow user to try login again

private void LoginAttempsTimeOut_Tick(object sender, EventArgs e)

{

    foreach(var user in loginAttemptsViolated.Keys)

    {

        loginAttemptsViolated.TryGetValue(user, out var date);

        TimeSpan span = DateTime.Now.Subtract(date);

        if(span.TotalMinutes > TimeOutInMinutes)

        {

            loginAttempsPerUser[user] = 0;

            loginAttemptsViolated.Remove(user);

            loginAttempsPerUser.Remove(user); 

        }

    }

}


private void btn_login_Click(object sender, EventArgs e)

{

        var obj = new Usercontrols.SIMSMain();

        obj.Dock = DockStyle.Fill;


        conn.Open();

        SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab", conn);

        selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);

        selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);

        SqlDataReader dataReader;

        dataReader = selectCommand.ExecuteReader();

        var count = 0;


        while (dataReader.Read())

        {

            count = count + 1;

        }

        if(loginAttemptsViolated.ContainsKey(txt_username.Text))

        {

           MetroMessageBox.Show("Login attempts is more than 3.");

        }

        else if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))

        {

            MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }

        else

        {

            if (count == 1)

            {

                MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.Hide();

                this.Parent.Controls.Add(obj);

            }

            else if (count == 3)

            {

                count++;

                MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            else

            {

                //if user cannot login, increase login attempts

                if(!loginAttempsPerUser.ContainsKey(txt_username.Text))

                   loginAttempsPerUser.Add(txt_username.Text, 1);


                loginAttempsPerUser[txt_username.Text]++;

                if(loginAttempsPerUser[txt_username.Text] > 2)

                {

                    // if login attempts > 2 set a 15 min timeout till user 

                    // cant login

                    if(!loginAttemptsViolated.ContainsKey(txt_username.Text))

                       loginAttemptsViolated.Add(txt_username.Text, DateTime.Now);

                }

                MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);

            }

        }

        conn.Close();

    }


}

也有超時,因為如果用戶違反了登錄計數(shù),則必須稍后再給他/她一次機(jī)會(例如 15 分鐘后)。


查看完整回答
反對 回復(fù) 2021-11-14
?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗 獲得超10個贊

您需要將計數(shù)變量聲明為全局變量。


    int count = 1;

    private void btn_login_Click(object sender, EventArgs e)

    {

        var obj = new Usercontrols.SIMSMain();

        obj.Dock = DockStyle.Fill;


        conn.Open();

        SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab", conn);

        selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);

        selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);

        SqlDataReader dataReader;

        dataReader = selectCommand.ExecuteReader();

        var counter = 0; //to check if there is data

        while (dataReader.Read())

        {

            counter++;

        }

        if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))

        {

            MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }

        else

        {

            if (counter == 1)

            {

                MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.Hide();

                this.Parent.Controls.Add(obj);

            }

            else if (count == 3)

            {

                count++;

                MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            else

            {

                count++;

                MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);

            }

        }

        conn.Close();

    }


查看完整回答
反對 回復(fù) 2021-11-14
?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗 獲得超6個贊

您已經(jīng)在使用SqlDataReader.Read()簡單的 else 可以處理沒有結(jié)果返回的情況。您已經(jīng)在使用SqlDataReader.Read()簡單的 else 可以處理沒有結(jié)果返回的情況。


int failAttempt  = 0;


private void btn_login_Click(object sender, EventArgs e)

{


    // Step 1: Check if inputs are valid

    if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))

    {

        MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        return;

    }



    // Step 2: Check if there is one user with this password/login

    var obj = new Usercontrols.SIMSMain();

    obj.Dock = DockStyle.Fill;


    conn.Open();

    SqlCommand selectCommand = new SqlCommand("Select * from admin_access where Username=@admin AND Password=@eyelab",conn);

    selectCommand.Parameters.AddWithValue("@admin", txt_username.Text);

    selectCommand.Parameters.AddWithValue("@eyelab", txt_password.Text);

    SqlDataReader dataReader = selectCommand.ExecuteReader();


    int numberMatch=0;


    while(dataReader.Read()){

        numberMatch++;

    }

    conn.Close();



    // Step 3: Fail Handle


    if(numberMatch==1){ // Success

        failAttempt  = 0;


        MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

        this.Hide();

        this.Parent.Controls.Add(obj);

    }

    else { // Fail 0 or more than one

        failAttempt ++;


        if (failAttempt == 3)

        {

            MetroMessageBox.Show(this, "Super Attempt", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        else

        {

            MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);

        }

    }


查看完整回答
反對 回復(fù) 2021-11-14
  • 3 回答
  • 0 關(guān)注
  • 242 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號