AQA · GCSE · Computer Science · Higher
Looping credential authentication in C#
A program is to be written to authenticate a username and password entered by the user. Figure 9 shows the only two pairs of valid usernames and passwords.
| Username | Password |
|---|---|
| Yusuf5 | 33kk |
| Mary80 | af5r |
Write a C# program to authenticate a username and password. Get the user to enter both values; display Access denied if the pair is invalid; display Access granted if the pair is valid; and repeat until a valid pair is entered. Use meaningful variable name(s) and C# syntax.
Write your answer first. You can study the marking guidance whenever you need it.
Study the marking See what earns credit and compare it with a full-mark answer.
Marking points
- 1 Uses meaningful variable names.
- 2 Uses an indefinite loop that continues validation until success.
- 3 Reads username and password into two variables.
- 4 Correctly pairs at least one username with its own password.
- 5 Correctly covers both valid credential pairs with quoted strings.
- 6 Re-reads both values after an invalid attempt.
- 7 Places Access denied and Access granted on the correct execution paths.
Full-mark answer
string username = Console.ReadLine(); string password = Console.ReadLine(); while ((username != "Yusuf5" || password != "33kk") && (username != "Mary80" || password != "af5r")) { Console.WriteLine("Access denied"); username = Console.ReadLine(); password = Console.ReadLine(); } Console.WriteLine("Access granted");
Why this answer loses marks
The program accepts any listed username with any listed password.
Each username must remain paired with its own password; separate membership checks would grant access to a mismatched pair.