Winform 实现记住密码和自动登录

一般的软件都有记住密码和自动登录功能,所以今天说一种winform的记住方式。

效果:

在这里插入图片描述
关闭软件,再次打开时,密码已经填写好了,不需要输入。
在这里插入图片描述
点击登录也能校验成功,完美实现登录记住账号密码。

自动登录:

在这里插入图片描述
勾选上自动登录后这个界面就不会显示了,每次打开软件是直接进入了main界面。

代码:

引用:
using System.Configuration;

app.config
<appSettings>
  <add key="autoLogin" value=""/> 
  <add key="rememberMe" value=""/>
  <add key="userName" value=""/>
  <add key="passWord" value=""/>
</appSettings>

在这里插入图片描述

login.cs

在之前的博客中代码的基础之上,添加代码
private void Login_Focus(object sender, EventArgs e)
{
    loginTextBoxUne.Focus(); // 获取输入账号焦点
    // 账号默认记住
    this.loginTextBoxUne.Text = ConfigurationManager.AppSettings["userName"];
    //如果记住密码为true 那么把值赋给文本框
    if (ConfigurationManager.AppSettings["rememberMe"].Equals("true"))
    {
        this.loginTextBoxPwd.Text = ConfigurationManager.AppSettings["passWord"];
        loginCheckBoxUne.Checked = true;
    }
    //如果是自动登录  那么拿获取 配置文件中的账号密码  然后到数据库里边查询 登录
    if (ConfigurationManager.AppSettings["autoLogin"].Equals("true"))
    {
        loginCheckBoxIs.Checked = true;
        loginButton_Click(sender, e);
    }
}


private void loginButton_Click(object sender, EventArgs e)
{
    string uneText = this.loginTextBoxUne.Text.Trim();
    string pwdText = this.loginTextBoxPwd.Text.Trim();
    // loginTextBoxUne.Focus(); // 获取输入账号焦点
    if (uneText.Equals(""))
    {
        MessageBox.Show("账号不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        this.loginTextBoxUne.Focus();
    }
    else if (pwdText.Equals(""))
    {
        MessageBox.Show("密码不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        this.loginTextBoxPwd.Focus();
    }
    else if (uneText == "admin" && pwdText == "admin")
    {
        // 记住账号密码 自动登录
        Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        cfa.AppSettings.Settings["userName"].Value = uneText; // 账号(默认记住)
        if (this.loginCheckBoxIs.Checked)
        {
            cfa.AppSettings.Settings["autoLogin"].Value = "true"; // 自动登录
            cfa.AppSettings.Settings["rememberMe"].Value = "true"; // 自动赋值
            cfa.AppSettings.Settings["passWord"].Value = pwdText; // 密码
        }
        else
        {
            if (this.loginCheckBoxUne.Checked)
            {
                cfa.AppSettings.Settings["autoLogin"].Value = "false"; // 自动登录
                cfa.AppSettings.Settings["rememberMe"].Value = "true"; // 自动赋值
                cfa.AppSettings.Settings["passWord"].Value = pwdText; // 密码
            }
            else
            {
                cfa.AppSettings.Settings["autoLogin"].Value = "false"; // 自动登录
                cfa.AppSettings.Settings["rememberMe"].Value = "false"; // 自动赋值
                cfa.AppSettings.Settings["passWord"].Value = ""; // 密码
            }
        }
        cfa.Save(); // 保存数据
        // 记录完数据,提示登录成功
        MessageBox.Show("  登录成功!  ", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        DialogResult = DialogResult.OK;
        this.Close(); // 登录成功关闭当前页面,启动新页面
    }
    else
        MessageBox.Show("登录失败,账号或密码错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    // Application.Run(new Login());
    if (ConfigurationManager.AppSettings["autoLogin"].Equals("true"))
    {
        Application.Run(new Main()); // 如果以后默认自动登录,则直接打开主窗体
    }
    else
    {
        Login login = new Login();
        if (login.ShowDialog() == DialogResult.OK)
        {
            login.Dispose();
            Application.Run(new Main());
        }
        else
        {
            login.Dispose();
            return;
        }
    }
}

demo下载
提取码:rtsh

以上就是winform自动自主账号密码和自动登录功能的实现.

转载请注明出处!

  • 12
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
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() 方法用于验证用户输入的账号和密码是否正确。需要注意的是,在实现自动登录功能时,需要先实现记住密码功能,否则自动登录时无法读取到密码信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值