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自动自主账号密码和自动登录功能的实现.

转载请注明出处!

猜你喜欢

转载自blog.csdn.net/weixin_42614447/article/details/86677488