Anjjie_Hi  

本地保存登录账号实现忘记密码及自动登录

  1  #region 删除本地自动登录及记住密码信息
  2         /// <summary>
  3         /// 删除本地自动登录及记住密码信息
  4         /// </summary>
  5         private void DeleteSelfMotionLoginOrRememberPwd()
  6         {
  7             if (cbAutomatic.Checked == false)
  8             {
  9                 if (Directory.Exists("SelfMotionLogin"))
 10                 {
 11                     if (File.Exists(@"SelfMotionLogin/SelfMotionLoginInfo.xml"))
 12                     {
 13                         File.Delete(@"SelfMotionLogin/SelfMotionLoginInfo.xml");
 14                     }
 15                 }
 16             }
 17             if (cbRememberPwd.Checked == false)
 18             {
 19                 if (Directory.Exists("RememberPwd"))
 20                 {
 21                     if (File.Exists(@"RememberPwd\" + txtLoginName.Text + ".xml"))
 22                     {
 23                         File.Delete(@"RememberPwd\" + txtLoginName.Text + ".xml");
 24                     }
 25                 }
 26             }
 27         }
 28         #endregion
 29 
 30         #region 保存自动登录及记住密码到本地事件方法
 31         /// <summary>
 32         /// 保存自动登录及记住密码到本地事件方法
 33         /// </summary>
 34         private void SelfMotionLoginOrRememberPwd()
 35         {
 36 
 37             if (cbAutomatic.Checked && cbRememberPwd.Checked)
 38             {
 39                 XElement xml = new XElement(
 40                     new XElement("LoginInfo",
 41                          new XElement("SelfMotionLogin",
 42                             new XElement("UserName", txtLoginName.Text),
 43                              new XElement("UserPwd", txtLoginPwd.Text)
 44                         ))
 45                     );
 46                 xml.Save(@"SelfMotionLogin/SelfMotionLoginInfo.xml");
 47             }
 48             else if (cbRememberPwd.Checked)
 49             {
 50                 XElement xml = new XElement(
 51                     new XElement("LoginInfo",
 52                          new XElement("RememberPwd",
 53                             new XElement("UserName", txtLoginName.Text),
 54                              new XElement("UserPwd", txtLoginPwd.Text)
 55                         ))
 56                     );
 57                 xml.Save(@"RememberPwd/" + txtLoginName.Text + ".xml");
 58             }
 59         }
 60         #endregion
 61 
 62         #region 获取自动登录及记住密码信息
 63         #region 获取记住密码信息
 64         /// <summary>
 65         /// 获取记住密码信息
 66         /// </summary>
 67         private void GetRememberPwd()
 68         {
 69             if (Directory.Exists("RememberPwd"))
 70             {
 71                 if (File.Exists(@"RememberPwd/" + txtLoginName.Text + ".xml"))
 72                 {
 73                     XElement xml = XElement.Load(@"RememberPwd/" + txtLoginName.Text + ".xml");
 74                     var AllInfo = from info in xml.Elements("RememberPwd")
 75                                   select new Users
 76                                   {
 77                                       UserName = info.Element("UserName").Value,
 78                                       UseuPwd = info.Element("UserPwd").Value
 79                                   };
 80                     if (AllInfo != null)
 81                     {
 82                         foreach (Users l in AllInfo)
 83                         {
 84                             txtLoginName.Text = l.UserName;
 85                             txtLoginPwd.Text = l.UseuPwd;
 86                             cbRememberPwd.Checked = true;
 87                         }
 88                     }
 89                 }
 90             }
 91         }
 92         #endregion
 93 
 94         #region 获取本地自动登录
 95         /// <summary>
 96         /// 获取本地自动登录
 97         /// </summary>
 98         private void GetSelfMotionLogin()
 99         {
100             if (Directory.Exists("SelfMotionLogin"))
101             {
102                 if (File.Exists(@"SelfMotionLogin/SelfMotionLoginInfo.xml"))
103                 {
104                     XElement xml = XElement.Load(@"SelfMotionLogin/SelfMotionLoginInfo.xml");
105                     var AllInfo = from info in xml.Elements("SelfMotionLogin")
106                                   select new Users
107                                   {
108                                       UserName = info.Element("UserName").Value,
109                                       UseuPwd = info.Element("UserPwd").Value
110                                   };
111                     if (AllInfo != null)
112                     {
113                         foreach (Users l in AllInfo)
114                         {
115                             txtLoginName.Text = l.UserName;
116                             txtLoginPwd.Text = l.UseuPwd;
117                             cbAutomatic.Checked = true;
118                             var checkoutData = from a in am.SelectUsersAll()
119                                                where a.UserName == l.UserName
120                                                select a;
121                             Users getCheckoutData = new Users();
122                             foreach (var get in checkoutData)
123                             {
124                                 getCheckoutData.UserName = get.UserName;
125                                 getCheckoutData.UseuPwd = get.UseuPwd;
126                             }
127                             DialogResult dialogResult = MessageBox.Show("你确定要自动登录 [" + txtLoginName.Text + "]吗?", "自动登录提示",
128                                 MessageBoxButtons.OKCancel,
129                                 MessageBoxIcon.Asterisk);
130                             if (dialogResult == DialogResult.OK)
131                             {
132                                 if (txtLoginName.Text != getCheckoutData.UserName)
133                                 {
134                                     MessageBox.Show("自动登录账号 [" + txtLoginName.Text + "] 不存在!", "登录出错",
135                                         MessageBoxButtons.OK,
136                                         MessageBoxIcon.Error);
137                                     txtLoginName.Focus();
138                                     return;
139                                 }
140                                 if (txtLoginPwd.Text != getCheckoutData.UseuPwd)
141                                 {
142                                     MessageBox.Show("自动登录密码不正确!", "登录提示",
143                                        MessageBoxButtons.OK,
144                                        MessageBoxIcon.Asterisk);
145                                     txtLoginPwd.Focus();
146                                     return;
147                                 }  
148                                 //传值
149                                 List<Users> Userss = am.SelectUsersAll().FindAll((p) => p.UserName.IndexOf(txtLoginName.Text) >= 0);
150                               foreach (Users a in Userss)
151                               {
152                                   saveUsersInfo.Name = a.Name;
153                                   saveUsersInfo.userNo = a.UNo;
154                               }
155 
156                                 this.Hide();
157                                 FrmMain frm = new FrmMain();
158                                 frm.Show();
159                                 //List<Users> Users = am.SelectUsersByconn(txtLoginName.Text);
160                                 //foreach (Users a in Users)
161                                 //{
162                                 //    Ano = a.ANo;
163                                 //    Name = a.Name;
164                                 //}
165                             }
166                         }
167                     }
168                 }
169                 else
170                 {
171                     timeLogin.Enabled = false;
172                 }
173             }
174             else
175             {
176                 timeLogin.Enabled = false;
177             }
178         }
179         #endregion
180 
181         private void cbAutomatic_CheckedChanged(object sender, EventArgs e)
182         {
183             if (cbAutomatic.Checked)
184             {
185                 cbRememberPwd.Checked = true;
186                 return;
187             }
188         }
189         #endregion
View Code

 

posted on 2017-08-02 16:24  Anjjie_Hi  阅读(599)  评论(0编辑  收藏  举报