WPF使用App.Config的读写操作来实现记住密码功能

7 篇文章 0 订阅

 

 

 

首先在App.Config文件夹中加入以下代码:

 <appSettings>
    <add key="userName" value="admin"/>
    <add key="password" value="1"/>
    <add key="isRemember" value="true"/>
  </appSettings>

xaml代码:

<TextBox InputMethod.IsInputMethodEnabled="False" Margin="20 10" materialDesign:HintAssist.Hint="账号" x:Name="txtUserNumber" MaxLength="11"/>
<PasswordBox Margin="20 10" materialDesign:HintAssist.Hint="密码" x:Name="txtPassword"/>
<Grid Margin="20 0">
    <CheckBox Content="记住我" HorizontalAlignment="Left" x:Name="ckbRemember"/>                        
    <Button x:Name="btnRegister" Content="注册" FontFamily="Aa语文老师的字" Foreground="#fa7dac" Style="{StaticResource MaterialDesignFlatButton}" HorizontalAlignment="Right" Click="BtnRegister_Click"/>
</Grid>
<Button x:Name="btnLogin" Content="登录" Margin="20 30" Background="#cdafff" BorderBrush="#cdafff" Click="BtnLogin_Click"/>

控制器代码:

using Emperor_System.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Emperor_System.View.RegisterLogin
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();

            txtUserNumber.Text = GetSettingString("userNumber");
            txtPassword.Password = GetSettingString("password");
            if (GetSettingString("isRemember") == "true")
            {
                ckbRemember.IsChecked = true;
            }
            else
            {
                ckbRemember.IsChecked = false;
            }
        }

        EmperorSystem_WPFEntities myModel = new EmperorSystem_WPFEntities();

        //private void Button_Click(object sender, RoutedEventArgs e)
        //{
        //    if (Convert.ToBoolean(ckbRemember.IsChecked))
        //    {
        //        UpdateSettingString("userNumber", txtUserNumber.Text);
        //        UpdateSettingString("password", txtPassword.Password);
        //        UpdateSettingString("isRemember", "true");
        //        MessageBox.Show("登录并记住密码!");
        //    }
        //    else
        //    {
        //        UpdateSettingString("userNumber", "");
        //        UpdateSettingString("password", "");
        //        UpdateSettingString("isRemember", "");
        //        MessageBox.Show("登录不记住密码!");
        //    }

        //}

        /// <summary>
        /// 读取客户设置
        /// </summary>
        /// <param name="settingName"></param>
        /// <returns></returns>
        public static string GetSettingString(string settingName)
        {
            try
            {
                string settingString = ConfigurationManager.AppSettings[settingName].ToString();
                return settingString;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 更新设置
        /// </summary>
        /// <param name="settingName"></param>
        /// <param name="valueName"></param>
        public static void UpdateSettingString(string settingName, string valueName)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (ConfigurationManager.AppSettings[settingName] != null)
            {
                config.AppSettings.Settings.Remove(settingName);
            }
            config.AppSettings.Settings.Add(settingName, valueName);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        //页面数据清空
        private void EmptyControl()
        {
            txtUserNumber.Text = string.Empty;
            txtPassword.Password = string.Empty;
        }

        //注册按钮(转到注册页面)
        private void BtnRegister_Click(object sender, RoutedEventArgs e)
        {
            Register myRegister = new Register();

            myRegister.Show();
            this.Close();
        }

        private void BtnLogin_Click(object sender, RoutedEventArgs e)
        {
            int InttxtUserNumber = txtUserNumber.Text.Length;
            String StrtxtUserNumber = txtUserNumber.Text;
            String intPassword = txtPassword.Password;
            try
            {
                //查询该账号
                var selectBagroundUser = (from dbBagroundUser in myModel.PW_BagroundUser
                                          where dbBagroundUser.UserNumber == StrtxtUserNumber.Trim()
                                          select new
                                          {
                                              dbBagroundUser.BagroundUserID,
                                              dbBagroundUser.UserNumber,
                                              dbBagroundUser.Password
                                          }).Single();
                if(intPassword == selectBagroundUser.Password)
                {
                    int BagroundUserId1 = selectBagroundUser.BagroundUserID;
                    string userNumber1 = txtUserNumber.Text;
                    //登录成功,跳到主页面
                    MainInterface myLogin = new MainInterface(BagroundUserId1, userNumber1);
                    myLogin.BagroundUserId = selectBagroundUser.BagroundUserID;
                    myLogin.userNumber = txtUserNumber.Text;
                    myLogin.Show();

                    if (Convert.ToBoolean(ckbRemember.IsChecked))
                    {
                        UpdateSettingString("userNumber", txtUserNumber.Text);
                        UpdateSettingString("password", txtPassword.Password);
                        UpdateSettingString("isRemember", "true");
                        this.Close();
                    }
                    else
                    {
                        UpdateSettingString("userNumber", "");
                        UpdateSettingString("password", "");
                        UpdateSettingString("isRemember", "");
                        this.Close();
                    }

                }
                else
                {
                    MessageBox.Show("您输入的或密码不正确!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }

            }
            catch (Exception)
            {
                MessageBox.Show("您输入的帐号不存在!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                //页面控件初始化
                EmptyControl();
            }
        }
    }
}

参考链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值