码迷,mamicode.com
首页 > 其他好文 > 详细

用户登录标准模式

时间:2014-06-12 22:49:13      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:winform   style   class   blog   code   java   

bubuko.com,布布扣
 1     static class Program
 2     {
 3         /// <summary>
 4         /// 应用程序的主入口点。
 5         /// </summary>
 6         [STAThread]
 7         static void Main()
 8         {
 9             Application.EnableVisualStyles();
10             Application.SetCompatibleTextRenderingDefault(false);
11 
12             //ShowDialog是阻塞的,此窗口不关闭,下面代码就不会执行
13             Login login = new Login();
14 
15             if (login.ShowDialog() == DialogResult.OK)//根据窗口login的返回值判定,是否进入主窗口
16             {
17                 //Application.Run启动的是主窗口,主窗口关闭程序就退出了。
18                 //不能将Login放在这里
19                 Application.Run(new FormMain());
20 
21             }
22 
23         }
24     }
bubuko.com,布布扣
bubuko.com,布布扣
 1         private void btnLog_Click(object sender, EventArgs e)//点击登录按钮发生时间
 2         {
 3             //UI非常简单,只负责收集用户输入的呈现输出
 4             if (new T_SeatsBLL().ValidateUser(txtName.Text, txtPwd.Text))//通过用户名遍历数据库,是否存在用户名,密码是否正确
 5             {
 6                 //通过BLL层获得当前登录用户的ID,保存到全局变量中 
 7                 //新建一个Globals类,有CurrentSeatID 属性,保存当前用户的ID,用于修改密码
 8                 T_Seats seat = new T_SeatsBLL().GetByUserName(txtName.Text);//通过用户名查询用户模型
 9                 Globals.CurrentSeatID =(int)seat.Id;
10 
11                 DialogResult=DialogResult.OK;
12 
13                 Close();
14                 //MessageBox.Show("OK");
15             }
16             else
17             {
18                 MessageBox.Show("名户名不存在或密码错误");
19             }
20         }    
bubuko.com,布布扣
bubuko.com,布布扣
        //凸显了BLL不只是转发DAL这么简单,可以有复杂的判断
        public bool ValidateUser(string username, string pwd)
        {
            T_SeatsDAL dal = new T_SeatsDAL();
            T_Seats seat = dal.GetByUserName(username);
            if (seat == null)
            {
                return false;
            }
            //比较用户输入密码的MD5值是否和数据库中的相同
            if (GetMD5(pwd) != seat.Pwd)
            {
                //不要在BLL层使用MessageBox,它是UI层东西
                return false;
            }
            else
            {
                return true;
            }
bubuko.com,布布扣
bubuko.com,布布扣
1         /// <summary>
2         /// 通过登录名查询用户模型
3         /// </summary>
4         /// <param name="username"></param>
5         /// <returns></returns>
6         public T_Seats GetByUserName(string username)
7         {
8             return new T_SeatsDAL().GetByUserName(username);
9         }
bubuko.com,布布扣
bubuko.com,布布扣
 1         //DataTable,DataSet,DataRow只应该在DAL中出现,DAL返回的应该是Model
 2         public T_Seats GetByUserName(string username)
 3         {
 4             DataTable dt = SqlHelper.ExecuteDataTable("select * from T_seats where UserName=@UserName", new SqlParameter("@UserName", username));
 5             if (dt.Rows.Count <= 0)//用户名不存在
 6             {
 7                 return null;
 8             }
 9             else if(dt.Rows.Count>1)//查询数据多于一条
10             {
11                 throw new Exception("用户名重复");
12 
13             }
14             else
15             {
16                 //DataRow dr = dt.Rows[0];
17                 //T_Seats seat = new T_Seats();
18                 //seat.Id = (int)dr["Id"];
19                 //seat.UserName = (string)dr["username"];
20                 //seat.Pwd = (string)dr["pwd"];
21                 //return seat; 
22 
23                 return ToModel(dt.Rows[0]);//返回数据模型
24             }
25 
26         }
bubuko.com,布布扣
bubuko.com,布布扣
1     /// <summary>
2     /// 放置全局变量的类,只是对Winform而言
3     /// 对于web开发,当前用户的ID放在Session,Cooiks里面保存
4     /// </summary>
5     class Globals
6     {
7         public static int CurrentSeatID;
8     }
bubuko.com,布布扣

修改密码模块:

bubuko.com,布布扣
 1         private void btnOk_Click(object sender, EventArgs e)//点击确定修改发生事件
 2         {
 3             string oldPwd = txtOldPwd.Text;
 4             string newPwd = txtNewPwd.Text;
 5             string newPwd2 = txtNewPwd2.Text;
 6 
 7             //判断两个密码是否一致是UI层的事情
 8             if (newPwd != newPwd2)
 9             {
10                 MessageBox.Show("两次输入不正确!");
11                 return;
12             }
13             //判断新旧密码是否一样
14             if (oldPwd == newPwd)
15             {
16                 MessageBox.Show("新密码不能与旧密码一样!");
17                 return;
18             }
19 
20             T_SeatsBLL bll = new T_SeatsBLL();
21             try
22             {
23                 //当前用户的ID存在着静态变量Globals.CurrentSeatID中,所以可以直接取出来用
24                 //静态变量就相当于全局唯一实例的变量
25                 bll.ChangePwd(Globals.CurrentSeatID, oldPwd, newPwd);
26                 MessageBox.Show("修改成功!");
27 
28                 //窗体关闭
29                 DialogResult = DialogResult.Cancel;
30             }
31             catch (Exception ex)
32             {
33                 //ex.ToString()是包含错误信息,调用栈堆的东西,用户看了会恐慌
34                 //ex.Message是new Exception("efwehewfrw")构造函数中的错误信息
35                 MessageBox.Show("发生错误," + ex.Message);//可能旧密码输入错误等异常
36                 return;
37             }
38             MessageBox.Show("修改成功!");
39         }
bubuko.com,布布扣
bubuko.com,布布扣
 1         /// <summary>
 2         /// 修改密码
 3         /// </summary>
 4         /// <param name="id">当前用户的ID</param>
 5         /// <param name="oldPwd">旧的密码</param>
 6         /// <param name="newPwd">新的密码</param>
 7         public void ChangePwd(int id, string oldPwd, string newPwd)
 8         {
 9             T_SeatsDAL dal=new T_SeatsDAL();
10 
11             //通过ID查询到对应的model。id保存在静态变量中
12             T_Seats seat = dal.Get(id);
13             if (seat == null)
14             {
15                 throw new Exception("用户名不存在!");
16             }
17             if(GetMD5(oldPwd) != seat.Pwd)
18             {
19                 //通过状态码返回更好
20                 throw new Exception("原密码输入错误!");
21             }
22             seat.Pwd = GetMD5(newPwd);
23             dal.Update(seat);
24             
25         }
bubuko.com,布布扣
bubuko.com,布布扣
 1         public T_Seats Get(int id)//通过ID查询模型
 2         {
 3             DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Seats  where id=@id",
 4             new SqlParameter("id", id));
 5             if (dt.Rows.Count > 1)
 6             { throw new Exception("more than 1 row was found"); }
 7             if (dt.Rows.Count <= 0) { return null; }
 8             DataRow row = dt.Rows[0];
 9             T_Seats model = ToModel(row);
10             return model;
11         }
bubuko.com,布布扣
bubuko.com,布布扣
 1         public static string GetMD5(string sDataIn)//得到字符串的MD5值
 2         {
 3             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
 4             byte[] bytValue, bytHash;
 5             bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
 6             bytHash = md5.ComputeHash(bytValue);
 7             md5.Clear();
 8             string sTemp = "";
 9             for (int i = 0; i < bytHash.Length; i++)
10             {
11                 sTemp += bytHash[i].ToString("X").PadLeft(2, 0);
12             }
13             return sTemp.ToLower();
14         }
bubuko.com,布布扣

 

 

 

用户登录标准模式,布布扣,bubuko.com

用户登录标准模式

标签:winform   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/skyl/p/3781506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!