public partial class LoginForm : DevExpress.XtraEditors.XtraForm
{
public LoginForm()
{
Logger.RecordInfo("初始化登录页面");
InitializeComponent();
}
//用户登录,验证账号密码
private void simpleButton1_Click(object sender, EventArgs e)
{
Boolean boo = CheckedPassword();
if (boo)
{
this.DialogResult = DialogResult.OK;
//this.Close();//当前页面关闭
}
else {
MessageBox.Show("用户名或密码不正确", "登录提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
}
/**
* 注册账号,必须输入管理员账号,密码
* */
private void simpleButton2_Click(object sender, EventArgs e)
{
string name = textEdit1.Text;
string password = textEdit2.Text;
if ("admin".Equals(name) && "cars@123".Equals(password))
{
RegisterForm registerForm = new RegisterForm();
registerForm.Show();
}
else {
MessageBox.Show("管理员账号,密码不匹配", "注册提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
}
//修改密码
private void simpleButton3_Click(object sender, EventArgs e)
{
ChangePassword changePassword = new ChangePassword();
changePassword.Show();
}
//对字符串进行加密
public static string CipherText(string proclaimText)
{
if (null == proclaimText || "".Equals(proclaimText)) {
return proclaimText;
}
byte[] proclaimBytes = UnicodeEncoding.BigEndianUnicode.GetBytes(proclaimText);
int proclaimBytesCount = proclaimBytes.Length;
byte[] cipherBytes = new byte[proclaimBytesCount * 2];
for (int i = 0; i < proclaimBytesCount; i += 2)
{
byte proclaimByte = proclaimBytes[i];
int upperByte = proclaimByte & 0xf0;
int lowerByte = proclaimByte & 0x0f;
upperByte = upperByte >> 4;
lowerByte = lowerByte << 4;
byte proclaimByte2 = proclaimBytes[i + 1];
int upperByte2 = proclaimByte2 & 0xf0;
int lowerByte2 = proclaimByte2 & 0x0f;
upperByte2 |= upperByte;
lowerByte2 |= lowerByte;
cipherBytes[i * 2] = 0x4e;
cipherBytes[i * 2 + 1] = Convert.ToByte(upperByte2);
cipherBytes[(i + 1) * 2] = 0x4f;
cipherBytes[(i + 1) * 2 + 1] = Convert.ToByte(lowerByte2);
}
return UnicodeEncoding.BigEndianUnicode.GetString(cipherBytes, 0, cipherBytes.Length);
}
//对字符串进行解密
public static string DoProclaimText(string CipherText)
{
if (null == CipherText || "".Equals(CipherText))
{
return CipherText;
}
byte[] cipherBytes = UnicodeEncoding.BigEndianUnicode.GetBytes(CipherText);
int cipherBytesCount = cipherBytes.Length;
byte[] proclaimBytes = new byte[cipherBytesCount / 2];
for (int i = 0; i < cipherBytesCount; i += 4)
{
byte cipherByte1 = cipherBytes[i + 1];
byte cipherByte2 = cipherBytes[i + 3];
int lowerByte = (cipherByte1 & 0x0f) << 4;
int upperByte = cipherByte1 & 0xf0;
int lowerByte2 = cipherByte2 & 0x0f;
int upperByte2 = (cipherByte2 & 0xf0) >> 4;
proclaimBytes[i / 2] = Convert.ToByte(lowerByte | upperByte2);
proclaimBytes[i / 2 + 1] = Convert.ToByte(upperByte | lowerByte2);
}
return UnicodeEncoding.BigEndianUnicode.GetString(proclaimBytes, 0, proclaimBytes.Length);
}
//校验密码
private Boolean CheckedPassword() {
string name = textEdit1.Text;
string password = textEdit2.Text;
if ("".Equals(name) || "".Equals(password))
{
MessageBox.Show("用户名或者密码不能为空", "登录提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
else
{
name = CipherText(name);
password = CipherText(password);
string strFilePath = AppDomain.CurrentDomain.BaseDirectory;
StreamReader sr = new StreamReader(strFilePath + "\\password.txt");
string line = sr.ReadLine();
while (null != line && !"null".Equals(line))
{
if (line.Contains(name))
{
string pass = line.Split(' ')[1]; //获得密码
if (password.Equals(pass))
{
sr.Close();
//密码正确,进入下一个页面
return true;
//break;
}
}
line = sr.ReadLine();
}
sr.Close();
}
return false;
}
}
其中有对密码的加密功能,只是在本地保存了一个文本文件,没有使用数据库,如果使用数据库的话,那就存在数据库里,用不用加密都可以。
public partial class RegisterForm : Form
{
public RegisterForm()
{
Logger.RecordInfo("注册账号");
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
string password = textBox2.Text;
string passwordCopy = textBox3.Text;
if (name.Length > 5 && password.Length > 5 && passwordCopy.Length > 5)
{
if (password.Equals(passwordCopy))
{
name = LoginForm.CipherText(name); //对账号密码进行加密
password = LoginForm.CipherText(password);
string strFilePath = AppDomain.CurrentDomain.BaseDirectory;
StreamReader sr = new StreamReader(strFilePath + "\\password.txt");
string line = sr.ReadLine();
while (line != null)
{
if (line.Contains(name))
{
MessageBox.Show("账号已存在,请重新输入", "注册提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
return;
}
line = sr.ReadLine();
}
sr.Close();
StreamWriter sw = File.AppendText(strFilePath + "\\password.txt");
sw.WriteLine(name + " " + password);
sw.Close();
MessageBox.Show("注册成功");
this.Close();
}
else {
MessageBox.Show("两次输入密码不相同", "注册提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
}
else {
MessageBox.Show("账号,密码长度不合法", "注册提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
}
}
public partial class ChangePassword : Form
{
public ChangePassword()
{
Logger.RecordInfo("修改密码界面");
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
string oldPassword = textBox2.Text;
string newPassword_1 = textBox3.Text;
string newPassword_2 = textBox4.Text;
if (oldPassword.Length < 6 || newPassword_1.Length < 6 || newPassword_2.Length < 6 || name.Length < 6) {
MessageBox.Show("账号或密码长度不合法!", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
return;
}
if (!newPassword_1.Equals(newPassword_2)) {
MessageBox.Show("两次输入密码不一致!", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
return;
}
if (oldPassword.Equals(newPassword_1)) {
MessageBox.Show("新旧密码不能相同!", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
return;
}
string strFilePath = AppDomain.CurrentDomain.BaseDirectory;
StreamReader sr = new StreamReader(strFilePath + "\\password.txt");
string text = "";
string line = sr.ReadLine();
while (null != line)
{
text = text + line + "\r\n";
line = sr.ReadLine();
}
sr.Close();
if (text.Contains(CipherText(name) + " " + CipherText(oldPassword)))
{
text = text.Replace(CipherText(name) + " " + CipherText(oldPassword), CipherText(name) + " " + CipherText(newPassword_1));
StreamWriter sw = new StreamWriter(strFilePath + "\\password.txt");
sw.Write(text);
sw.Close();
MessageBox.Show("密码修改成功");
this.Close();
}
else {
MessageBox.Show("旧账号密码不匹配!", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
}
}
}
再来一个main方法的登录验证机制:
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm loginForm = new LoginForm();
if (loginForm.ShowDialog() == DialogResult.OK) {
Application.Run(new MainForm());
}
//Application.Run(new LoginForm());
}
}
全部完成!!!