C#登录窗口一些小技巧应用,防御性编程以及加密
2019-04-11 10:24
399 查看
实例:
[code]private void LoginBtn_Click(object sender, EventArgs e) { //判断用户名和密码输入情况 if (UserName.Text.Length <= 0) { MessageBox.Show("用户名为空,请输入用户名!"); return; } if (txtPassword.Text.Length <= 0) { MessageBox.Show("密码不能为空!"); return; } try { //string conn = dbconfig.readxml();方法一 string conn = configxml.xmlconn.readxml(); //使用xml读取配置进行数据库连接替代20151023--(使用app.config默认配置程序连接数据库配置) //string conn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; //使用db.config就是上面这个方法来写,使用ConfigurationManager但要先引用System.configuration;方法二 Helpers.SqlHelper db = new Helpers.SqlHelper(conn); DataTable dt = db.ExecuteDataTable("select Id,username,password,ErrorTimes,sign_time from T_users where username = @userName", new SqlParameter("@username", UserName.Text)); //防御性编程,判断过滤条件 if (dt.Rows.Count <= 0) { MessageBox.Show("输入的用户名不存在或密码错误,请重新输入!"); return; } if (dt.Rows.Count > 1) { MessageBox.Show("用户名重复!"); return; } //读取行记录 DataRow row = dt.Rows[0]; //显性转换类型 string dbpassword = (string)row["password"]; long Id = (long)row["Id"]; int ErrorTimes = (int)row["ErrorTimes"]; DateTime dbtime = (DateTime)row["sign_time"]; //用Timespan函数计算间隔时间差 DateTime TxtTime = DateTime.Parse(DateTime.Now.ToString()); TimeSpan timespan = TxtTime - dbtime; if (ErrorTimes >= 5) { if (timespan.TotalMinutes > 5) //如果超过5分钟即解除锁定 { db.ExecuteNonQuery("update T_users set ErrorTimes = 0 where Id = @Id", new SqlParameter("@Id", Id)); } else { MessageBox.Show("因密码输错次数过多,该账号已经被禁止登录,请与管理员进行联系或稍候再试!"); } } else { if (dbpassword != Encrypt.GetMD5_32(txtPassword.Text)) { { db.ExecuteNonQuery("update T_users set ErrorTimes = ErrorTimes+1,sign_time = '" + DateTime.Now + "' where Id = @Id", new SqlParameter("@ID", Id)); MessageBox.Show("密码错误,请重新输入!"); txtPassword.Clear();//输入错误自动清空输入框 return; } } else { db.ExecuteNonQuery("update T_users set ErrorTimes = 0 where Id = @Id", new SqlParameter("@Id", Id)); GlobalInfo.LoginUser = UserName.Text.ToString(); MISMain d = new MISMain(); d.Show(); this.Hide(); } } } catch { ServerConfig sc = new ServerConfig(); sc.Show(); } }
相关文章推荐
- C#中检测某个类(方法、程序集等各种部分)是否应用了指定的特性以及对特性的一些简单操作
- 使用.net框架应用C#语言开发窗口程序的一些注意事项
- C#登录窗口简单应用
- 红黑树、B(+)树、跳表、AVL等数据结构,应用场景及分析,以及一些英文缩写
- 为安卓应用添加手势密码功能,遇到的一些问题以及解决方法
- c# winform 应用编程代码总结 13
- 一些小技巧C# 和SQL
- 几个C#编程的小技巧 (二)
- Java实现冒泡排序以及优化冒泡排序的一些小技巧
- 关于Javascript中this的指向以及一些应用
- 编程打开一些特殊的系统窗口(如控制面板等)
- C#使用Xamarin开发可移植移动应用(5.进阶篇显示弹出窗口与通讯中心)附源码
- C#小技巧 发现的一些不太注意的小功能
- C#下WinForm编程:登录窗体的设计
- C#实现登录窗口(不用隐藏)
- 有关python的字典以及对象什么的一些小技巧
- 几个C#编程的小技巧
- [置顶] VC++界面编程之--仿Facebook透明登录窗口
- 如何动态加载控件以及插件编程思想(C#)
- C#委托的一些应用,个人肤浅的一些操作