Click here to Skip to main content
15,913,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey i have created a program which the user login if the user enter wrong password it will prompt a message up to 3 attempts

such as 1 - 2 attempts left, 2 - 1 attempt left, 3 - you have no attempt left and your password been blocked

here the code
C#
int attempt = 3;
if (!pin.Equals("...") && attempts > 1)
                    {
                        attempts--;
                        MessageBox.Show("Wrong pin number! " + attempts + " attempts left");
                        
                    }
                    else if (!pin.Equals("...")&& attempts==1)
                    {
                        MessageBox.Show("wrong password! your account been blocked");
                       
                    }


problem i got is it assign it to every username where i want each indivdual username to have 3 attempts each before their password is blocked.

how would i do this?
Posted
Comments
stefanere2k9 22-Feb-12 17:48pm    
what if the username are all in the database? or would i still have to put indivdual usernames in that piece of code?
Shahin Khorshidnia 22-Feb-12 18:27pm    
Bad Idea. Don't do this kind of securities in client side. If the application is a windows-based then it can be reflected by user.
Do the authentication with a stored procedure in database.

Declare @count as int

Select @count = COUNT(UserName) From
[User]
Where UserName = @UserName And
Attempts >0

1 solution

You could use a Dictionary<TKey, TValue>[^] where the key is the username and the value is the attempts that are left for this user.
C#
Dictionary<String, int> usersAndAttempts = new Dictionary<String, int>();
if (!usersAndAttempts.ContainsKey(userName))
{
   // Add the user and his three attempts to the dictionary.
}
// Your code here, but gettings the attempts from the dictionary where the key is the specified user.
Of course this will give every user three logins until he restarts your application, after which his three attempts will be reset. To permanently block the user you'd have to write something to a database which indicates that the specified user is locked.
Hope it helps! :)
 
Share this answer
 
v2
Comments
stefanere2k9 1-Mar-12 12:33pm    
could i use an array here
such as int[] attempts = new new[5]
{
not quite sure how to call stuff from database?
]
Sander Rossel 1-Mar-12 14:58pm    
Nope, your best and easiest bet is really a dictionary.
You can Google how to work with a database. If it is necessary for your application.
stefanere2k9 1-Mar-12 15:29pm    
is this correct?
Dictionary<string, int=""> attemptsmade = new Dictionary<string, int="">();
if (!attemptsmade.ContainsKey("cardNumber"))
{
attemptsmade.Add("1234", 3);
attemptsmade.Add("3455", 3);
attemptsmade.Add("4343", 3);
attemptsmade.Add("4432", 3);
attemptsmade.Add("5454", 3);
}

would i stay be able to do the messagebox the same way?
Sander Rossel 1-Mar-12 15:50pm    
Dictionary<string, int>
Your key is probably the cardnumber here. So that one is unique, you can only add it once. You can update the value every time a user made an attempt to log in.
Check the MSDN documentation for more info and examples on Dictionary<TKey, TValue>
No idea what you mean with the TextBox...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900