C# Winform 登录中的忘记密码及自动登录
2024-08-26 10:50:58
本地保存登录账号实现忘记密码及自动登录
#region 删除本地自动登录及记住密码信息
/// <summary>
/// 删除本地自动登录及记住密码信息
/// </summary>
private void DeleteSelfMotionLoginOrRememberPwd()
{
if (cbAutomatic.Checked == false)
{
if (Directory.Exists("SelfMotionLogin"))
{
if (File.Exists(@"SelfMotionLogin/SelfMotionLoginInfo.xml"))
{
File.Delete(@"SelfMotionLogin/SelfMotionLoginInfo.xml");
}
}
}
if (cbRememberPwd.Checked == false)
{
if (Directory.Exists("RememberPwd"))
{
if (File.Exists(@"RememberPwd\" + txtLoginName.Text + ".xml"))
{
File.Delete(@"RememberPwd\" + txtLoginName.Text + ".xml");
}
}
}
}
#endregion #region 保存自动登录及记住密码到本地事件方法
/// <summary>
/// 保存自动登录及记住密码到本地事件方法
/// </summary>
private void SelfMotionLoginOrRememberPwd()
{ if (cbAutomatic.Checked && cbRememberPwd.Checked)
{
XElement xml = new XElement(
new XElement("LoginInfo",
new XElement("SelfMotionLogin",
new XElement("UserName", txtLoginName.Text),
new XElement("UserPwd", txtLoginPwd.Text)
))
);
xml.Save(@"SelfMotionLogin/SelfMotionLoginInfo.xml");
}
else if (cbRememberPwd.Checked)
{
XElement xml = new XElement(
new XElement("LoginInfo",
new XElement("RememberPwd",
new XElement("UserName", txtLoginName.Text),
new XElement("UserPwd", txtLoginPwd.Text)
))
);
xml.Save(@"RememberPwd/" + txtLoginName.Text + ".xml");
}
}
#endregion #region 获取自动登录及记住密码信息
#region 获取记住密码信息
/// <summary>
/// 获取记住密码信息
/// </summary>
private void GetRememberPwd()
{
if (Directory.Exists("RememberPwd"))
{
if (File.Exists(@"RememberPwd/" + txtLoginName.Text + ".xml"))
{
XElement xml = XElement.Load(@"RememberPwd/" + txtLoginName.Text + ".xml");
var AllInfo = from info in xml.Elements("RememberPwd")
select new Users
{
UserName = info.Element("UserName").Value,
UseuPwd = info.Element("UserPwd").Value
};
if (AllInfo != null)
{
foreach (Users l in AllInfo)
{
txtLoginName.Text = l.UserName;
txtLoginPwd.Text = l.UseuPwd;
cbRememberPwd.Checked = true;
}
}
}
}
}
#endregion #region 获取本地自动登录
/// <summary>
/// 获取本地自动登录
/// </summary>
private void GetSelfMotionLogin()
{
if (Directory.Exists("SelfMotionLogin"))
{
if (File.Exists(@"SelfMotionLogin/SelfMotionLoginInfo.xml"))
{
XElement xml = XElement.Load(@"SelfMotionLogin/SelfMotionLoginInfo.xml");
var AllInfo = from info in xml.Elements("SelfMotionLogin")
select new Users
{
UserName = info.Element("UserName").Value,
UseuPwd = info.Element("UserPwd").Value
};
if (AllInfo != null)
{
foreach (Users l in AllInfo)
{
txtLoginName.Text = l.UserName;
txtLoginPwd.Text = l.UseuPwd;
cbAutomatic.Checked = true;
var checkoutData = from a in am.SelectUsersAll()
where a.UserName == l.UserName
select a;
Users getCheckoutData = new Users();
foreach (var get in checkoutData)
{
getCheckoutData.UserName = get.UserName;
getCheckoutData.UseuPwd = get.UseuPwd;
}
DialogResult dialogResult = MessageBox.Show("你确定要自动登录 [" + txtLoginName.Text + "]吗?", "自动登录提示",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk);
if (dialogResult == DialogResult.OK)
{
if (txtLoginName.Text != getCheckoutData.UserName)
{
MessageBox.Show("自动登录账号 [" + txtLoginName.Text + "] 不存在!", "登录出错",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
txtLoginName.Focus();
return;
}
if (txtLoginPwd.Text != getCheckoutData.UseuPwd)
{
MessageBox.Show("自动登录密码不正确!", "登录提示",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
txtLoginPwd.Focus();
return;
}
//传值
List<Users> Userss = am.SelectUsersAll().FindAll((p) => p.UserName.IndexOf(txtLoginName.Text) >= );
foreach (Users a in Userss)
{
saveUsersInfo.Name = a.Name;
saveUsersInfo.userNo = a.UNo;
} this.Hide();
FrmMain frm = new FrmMain();
frm.Show();
//List<Users> Users = am.SelectUsersByconn(txtLoginName.Text);
//foreach (Users a in Users)
//{
// Ano = a.ANo;
// Name = a.Name;
//}
}
}
}
}
else
{
timeLogin.Enabled = false;
}
}
else
{
timeLogin.Enabled = false;
}
}
#endregion private void cbAutomatic_CheckedChanged(object sender, EventArgs e)
{
if (cbAutomatic.Checked)
{
cbRememberPwd.Checked = true;
return;
}
}
#endregion
最新文章
- HTTP的长连接和短连接
- 移动端Web适配的两种做法思路总结
- MySQL数据库命名及设计规范
- ZKM混淆工具
- Erlang 程序引发共享内存 bug 的一个例子
- oracle 笔记
- Dubbo扩展点加载机制
- Java中list<;Object>;集合去重实例
- 在OC和Swift中使用IBDesignable/IBInspectable
- js中对象相关
- swoole 弹幕系统
- iOS开发之文件(分段)下载
- 201521123068《Java程序设计》第1周学习总结
- 2017-06-24(chgrp umask alias unalias)
- Cocos Creator—定制H5游戏首页loading界面
- hive 使用反射函数
- new/new[]和delete/delete[]是如何分配空间以及释放空间的
- redis 3.2.3的源码安装
- 【Python】打印object对象
- mysql limit 优化