开发者

Do action if button clicked for 3 times

开发者 https://www.devze.com 2023-04-12 21:37 出处:网络
I want to do some action if button clicked for 3 times. Just like if users enters a wrong pass开发者_开发问答word for 3 times the page the page must be redirected to another page or something.

I want to do some action if button clicked for 3 times. Just like if users enters a wrong pass开发者_开发问答word for 3 times the page the page must be redirected to another page or something.

How to do action in ASP.NET C# if button clicked for 3rd time?


To check if a user has taken an action fox "x" times you need to store it somehwere. If you're using a relation database you could for example call a table LoginAttempts and there you store all unsuccesfull logins. When you have that table you can build your logic against it.


I'd use a session variable.

Another solution could be having a column in the users table named LoginAttempt(int)(default 0), how I would use that column is

Let's say you have a table in your databese called TblUsers with these columns

  1. Id,
  2. UserName,
  3. Password,
  4. LoginAttempt.

And let's say you have two TextBoxes on your Login.aspx page

  1. TextBoxUserName,
  2. TextBoxPassword.

Let's say you have one record in your TblUsers like this

Id           : 1
UserName     : rammstein
Password     : ohnedich
LoginAttempt : 0

Now, you are in your Login.aspx.cs code

you have a method and in it you have

TblUsers user = new TblUsers();

And you have a bool login = false;. You've got the username from TextBoxUserName.Text, you check if a user with this username exists then if it exist you do the below code.

Let's follow this scenerio

The given user tried to login with

UserName:rammstein 
Password:duhast

Checked your database with username rammstein and found it, and took that record in your TblpUsers user then you checked whether user.Password and TextBoxPassword.Text matches.Well it won't match for the above example because

user.Password is ohnedich however TextBox.Password is duhast.This means the login is not successfull so you set false to your bool login.

Everything else belongs to the below code with if-else condition

if(user.LoginAttempt < 3){

      if(!login)
      {
          user.LoginAttempt = user.LoginAttempt + 1;
      }
      else
      {
          user.LoginAttempt = 0;
      }
}
else
{
  //do something
}


Multiple solutions:

1) Session variable (Okay solution)

2) Static class variable (Not a good solution)

3) DB record field (Best solution)

4) Pass flag variable back and forth between view and controller (not a very good idea).

5) Browser cookie (can be cleared)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号