How to set an access password for an application written in WPF? When you open the application for the first time, you enter a new password, at subsequent logins you enter this password. This should be implemented without using a database.
- Where to store the password? - Monk
- anywhere, maybe in resources. - endovitskiiy
- The main thing is not in the database - endovitskiiy
1 answer
Put the Password property of the string type in the application settings , as well as the IsPasswordSet property of the bool type (with an initial value of false ). Be sure to put in the user scope, not in the application scope!
Now you can use code like this:
if (!Properties.Settings.Default.IsPasswordSet) { Console.WriteLine("Set password: "); Properties.Settings.Default.Password = Console.ReadLine(); Properties.Settings.Default.IsPasswordSet = true; Properties.Settings.Default.Save(); } else { Console.WriteLine("Enter password: "); var password = Console.ReadLine(); if (password != Properties.Settings.Default.Password) Environment.Exit(1); } This solution, however, stores the password in clear text. If you want something more reliable, read the (cryptographic!) Password hash and store it, take the hash from the input and compare it when checking.
You can enhance password protection by encrypting your configuration, as indicated here .
However, solutions where password checking takes place on a system under the control of the user are fundamentally unsafe . A malicious user can replace the data in which you store the password with your own. Well, any program running under the control of the user can be hacked. Therefore, do not use this solution if security depends on the strength of password protection or access to important information is open.
- I have a SetPassCommand . I don’t understand how to click on the "Set" button (at the first entry) to pass two values to the CommandParameter at once (I need to set a password and a password confirmation on my form) to compare them. - endovitskiiy
- @endovitskiiy: Create a class with two fields, and pack both values into it? And pass it on. - VladD
- And how to pack two fields at once, because PasswordBox cannot be set to Binding - endovitskiiy
- 2I did this: stackoverflow.com/questions/9108044/… - endovitskiiy
- @endovitskiiy: Yeah, Tuple is also a good option. - VladD