winform登录记住用户和密码

用combobox控件来显示用户

  Dictionary<string, UserInfo> users = new Dictionary<string, UserInfo>();
  public void SavePW()//保存用户和密码
        {
            UserInfo user = new UserInfo();

            // 登录时 如果没有Data.bin文件就创建、有就打开
            FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
            BinaryFormatter bf = new BinaryFormatter();
            // 保存在实体类属性中
            user.LoginID = txtUser.Text.Trim();
            //保存密码选中状态
            if (checkBoxXpw.Checked)
                user.Pwd = txtPwd.Text.Trim();
            else
                user.Pwd = "";
            //选在集合中是否存在用户名 
            if (users.ContainsKey(user.LoginID))
            {
                //如果有清掉
                users.Remove(user.LoginID);
            }
            //添加用户信息到集合
            users.Add(user.LoginID, user);
            //写入文件
            bf.Serialize(fs, users);
            //关闭
            fs.Close();
        }


        public void OpenPW()//打开用户和密码
        {
            FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
            //fs.Seek(0, SeekOrigin.Begin);

            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                fs.Position = 0;
                //读出存在Data.bin 里的用户信息
                users = bf.Deserialize(fs) as Dictionary<string, UserInfo>;
                //循环添加到Combox1
                foreach (UserInfo user in users.Values)
                {
                    txtUser.Items.Add(user.LoginID);
                }

                //combox1 用户名默认选中第一个
                //if (txtUser.Items.Count > 0)
                //    txtUser.SelectedIndex = txtUser.Items.Count - 1;
            }
            fs.Close();
        }


 private void txtUser_SelectedValueChanged(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);

            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                users = bf.Deserialize(fs) as Dictionary<string, UserInfo>;
                for (int i = 0; i < users.Count; i++)
                {
                    if (txtUser.Text != "")
                    {
                        if (users.ContainsKey(txtUser.Text) && users[txtUser.Text].Pwd != "")
                        {
                            txtPwd.Text = users[txtUser.Text].Pwd;
                            checkBoxXpw.Checked = true;
                        }
                        else
                        {
                            txtPwd.Text = "";
                            checkBoxXpw.Checked = false;
                        }
                    }
                }
            }

            fs.Close();


        }
    }




[Serializable]
public class UserInfo
{
    public string LoginID
    {
        get;
        set;
    }

    public string Pwd
    {
        get;
        set ;
    }

}

WinForm 中实现记住密码和自动登录功能,需要将用户输入的账号和密码保存到本地,下次启动程序时自动读取已保存的信息进行登录。以下是一个简单的实现步骤: 1. 在登录页面添加“记住密码”和“自动登录”选项,并将用户输入的账号和密码保存到本地,可以使用配置文件、注册表、数据库等方式进行存储。 2. 在程序启动时检查本地是否保存用户登录信息,如果有则自动填充账号和密码,并触发登录按钮的 Click 事件进行自动登录。 3. 在用户手动退出登录时,清除本地保存登录信息。 下面是一个示例代码: ```csharp private void LoginForm_Load(object sender, EventArgs e) { // 读取保存登录信息 if (Properties.Settings.Default.RememberMe) { txtUsername.Text = Properties.Settings.Default.Username; txtPassword.Text = Properties.Settings.Default.Password; chkRememberMe.Checked = true; } if (Properties.Settings.Default.AutoLogin) { btnLogin.PerformClick(); } } private void btnLogin_Click(object sender, EventArgs e) { // 登录验证 if (ValidateUser(txtUsername.Text, txtPassword.Text)) { // 保存登录信息 if (chkRememberMe.Checked) { Properties.Settings.Default.Username = txtUsername.Text; Properties.Settings.Default.Password = txtPassword.Text; Properties.Settings.Default.RememberMe = true; Properties.Settings.Default.Save(); } else { Properties.Settings.Default.Username = ""; Properties.Settings.Default.Password = ""; Properties.Settings.Default.RememberMe = false; Properties.Settings.Default.Save(); } if (chkAutoLogin.Checked) { Properties.Settings.Default.AutoLogin = true; Properties.Settings.Default.Save(); } // 登录成功 this.DialogResult = DialogResult.OK; } else { // 登录失败 MessageBox.Show("登录失败,请检查账号和密码是否正确。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnLogout_Click(object sender, EventArgs e) { // 清除登录信息 Properties.Settings.Default.Username = ""; Properties.Settings.Default.Password = ""; Properties.Settings.Default.RememberMe = false; Properties.Settings.Default.AutoLogin = false; Properties.Settings.Default.Save(); // 退出登录 this.DialogResult = DialogResult.Cancel; } ``` 其中,Properties.Settings.Default 是一个应用程序配置文件,用于保存应用程序的配置信息。ValidateUser() 方法用于验证用户输入的账号和密码是否正确。需要注意的是,在实现自动登录功能时,需要先实现记住密码功能,否则自动登录时无法读取到密码信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值