在数据库中增加一个字段mac,将对应用户的mac信息或IP信息写入该字段,通过以下代码即可将账号和计算机绑定!
红色内容为md5加密密码的方法(需添加using System.Security.Cryptography;): string username = tb_username.Text.ToString().Trim(); //byte[] result = Encoding.Default.GetBytes(this.tb_password.Text.Trim()); //MD5 md5 = new MD5CryptoServiceProvider(); //byte[] output = md5.ComputeHash(result); //string userpass = BitConverter.ToString(output).Replace("-", ""); string userpass = tb_password.Text.ToString().Trim(); string sql = "select id,username,userpass,mac from p_users where username='" + username + "' and userpass='" + userpass + "'"; SqlCommand cmd = new SqlCommand(sql, conn); try { conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { string mac = getMacAddr_Local();
//string ip = GetIP();
if (mac != dr["mac"].ToString())
{ MessageBox.Show("请在正确的计算机上使用本程序!"); Application.Exit(); } else { id = dr["id"].ToString(); //name = dr["truename"].ToString(); //userrole = dr["userrole"].ToString(); this.Close(); }
}
else { MessageBox.Show("用户名或密码错误!", "错误", MessageBoxButtons.OK); } } catch (Exception ex) { ex.ToString(); } finally { conn.Close(); } |
转载于:https://blog.51cto.com/530007/1285633