C # Add the Remember password and automatic login function to the software login window

Source: Internet
Author: User

Let's take a look at the effect:

Recently I am working on a foreign trade import software, which is developed using the C/S architecture. For ease of use, this does not try to add the function of remembering the user name and password, and can be set to enable the software automatically upon startup, all in order to reduce the user's click operation.

There are multiple ways to save user information, such as saving it as a local file, xml, registry, and even worse, saving it to the database. I personally think this operation was a little bad before I logged on to the database. It is not good to go to the database to retrieve the password and put it back in the input box.

Finally, the Registry is used to save the information. In order to demonstrate that the password saved in the registry is not encrypted, you should continue to encrypt the password for security.

Private void ckbKeepInfo_CheckStateChanged (object sender, EventArgs e) {RegistryKey location = Registry. localMachine; RegistryKey soft = location. openSubKey ("SOFTWARE", true); // you can specify RegistryKey myPass = soft. createSubKey ("FTLiang"); myPass. setValue ("s1", tbUserName. text); myPass. setValue ("s2", tbPassword. text); myPass. setValue ("s3", ckbKeepInfo. checked );}


Automatic Login:

Private void ckbAutoStart_CheckStateChanged (object sender, EventArgs e) {if (ifFistIn = false) {RegistryKey location = Registry. localMachine; RegistryKey soft = location. openSubKey ("SOFTWARE", true); // you can specify RegistryKey myPass = soft. createSubKey ("FTLiang"); myPass. setValue ("s4", ckbAutoStart. checked); if (ckbAutoStart. checked) {string exeDir = Application. executablePath; // the absolute path of the program to be started. RegistryKey rk = Registry. localMachine; RegistryKey softWare = rk. openSubKey ("SOFTWARE"); RegistryKey microsoft = softWare. openSubKey ("Microsoft"); RegistryKey windows = microsoft. openSubKey ("Windows"); RegistryKey current = windows. openSubKey ("CurrentVersion"); RegistryKey run = current. openSubKey (@ "Run", true); // Add true here to get the write permission run. setValue ("FTStart", exeDir);} else {string exeDir = Application. executablePath; // the absolute path of the program to be started. RegistryKey rk = Registry. localMachine; RegistryKey softWare = rk. openSubKey ("SOFTWARE"); RegistryKey microsoft = softWare. openSubKey ("Microsoft"); RegistryKey windows = microsoft. openSubKey ("Windows"); RegistryKey current = windows. openSubKey ("CurrentVersion"); RegistryKey run = current. openSubKey (@ "Run", true); // Add true here to get the write permission run. deleteValue ("FTStart"); // you must add true here to get the write permission }}}

Initial form display:

Private void FmLogin_Load (object sender, EventArgs e) {// read from the Registry whether the user name and password are saved and automatically start configuration try {RegistryKey location = Registry. localMachine; RegistryKey soft = location. openSubKey ("SOFTWARE", false); // you can specify RegistryKey myPass = soft. openSubKey ("FTLiang", false); tbUserName. text = myPass. getValue ("s1 "). toString (); string s2 = myPass. getValue ("s2 "). toString (); bool ifSave = Convert. toBoolean (myPass. getValue ("s3"); ckbKeepInfo. checked = ifSave; bool ifSave2 = Convert. toBoolean (myPass. getValue ("s4"); ckbAutoStart. checked = ifSave2; if (ifSave) {tbPassword. text = s2;} else {tbPassword. text = "";} ifFistIn = false; // after the program is started, you can execute registry-related actions} catch (Exception ex) {// todo something }}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.