📄 formlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text ;
using System.Data .SqlClient ;
using BlueHill.BlueWindows.Security;
using System.Security .Cryptography ;
namespace Security
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FormLogin : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Button btnChange;
private System.Windows.Forms.Button btnOk;
private System.Windows.Forms.Button btnCancel;
private SqlConnection cn;
private SqlCommand cmd;
public string userid;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public FormLogin()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
cn = new SqlConnection("integrated security=SSPI;data source=(local);initial catalog=BlueHill");
cmd = new SqlCommand("select LoginName,Password,DeptID from tblEmployee where LoginName=@LoginName",cn);
cmd.Parameters.Add("@LoginName",SqlDbType.NVarChar,20);
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormLogin));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtUserName = new System.Windows.Forms.TextBox();
this.txtPassword = new System.Windows.Forms.TextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.btnChange = new System.Windows.Forms.Button();
this.btnOk = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 0;
this.label1.Text = "用户名";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(80, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(40, 23);
this.label2.TabIndex = 1;
this.label2.Text = "密码";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(152, 24);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(136, 21);
this.txtUserName.TabIndex = 2;
this.txtUserName.Text = "";
//
// txtPassword
//
this.txtPassword.Location = new System.Drawing.Point(152, 64);
this.txtPassword.Name = "txtPassword";
this.txtPassword.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(136, 21);
this.txtPassword.TabIndex = 3;
this.txtPassword.Text = "";
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(24, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
this.pictureBox1.TabIndex = 4;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(24, 64);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(32, 24);
this.pictureBox2.TabIndex = 5;
this.pictureBox2.TabStop = false;
//
// btnChange
//
this.btnChange.Location = new System.Drawing.Point(16, 104);
this.btnChange.Name = "btnChange";
this.btnChange.TabIndex = 6;
this.btnChange.Text = "修改密码";
this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
//
// btnOk
//
this.btnOk.Location = new System.Drawing.Point(112, 104);
this.btnOk.Name = "btnOk";
this.btnOk.TabIndex = 7;
this.btnOk.Text = "登陆";
this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(208, 104);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 8;
this.btnCancel.Text = "取消";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// FormLogin
//
this.AcceptButton = this.btnOk;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 147);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.btnChange);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FormLogin";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "登陆";
this.Load += new System.EventHandler(this.FormLogin_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FormLogin());
}
int i = 0;
private void btnOk_Click(object sender, System.EventArgs e)
{
string userid = txtUserName.Text;
if (txtPassword.Text.Length>=6)
{
//对输入的密码做HashMD5加密。
UnicodeEncoding UE=new UnicodeEncoding();
byte [] pwd = UE.GetBytes(this.txtPassword.Text);
SHA1Managed sha =new SHA1Managed();
byte[] HashedPwd = sha.ComputeHash(pwd);
cmd.Parameters["@LoginName"].Value = userid;
cn.Open ();
SqlDataReader dr=cmd.ExecuteReader();
if (dr.Read())
{
byte[] datapwd=(byte[])dr.GetValue(1);
if (UE.GetString(HashedPwd) == UE.GetString(datapwd))
{
FormMain mn=new FormMain();
mn.userid = userid;
mn.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("密码输入错误!");
txtPassword.Clear ();
i++;
if(i>=3)
{
MessageBox.Show("密码输入三次错误");
this.Close();
}
}
}
else
{
MessageBox.Show("此用户不存在!");
}
dr.Close();
cn.Close();
}
else
{
if(txtUserName.Text =="")
MessageBox.Show ("用户名不能用空!");
else
MessageBox.Show("密码长度应大于6位!");
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btnChange_Click(object sender, System.EventArgs e)
{
string userid = txtUserName.Text;
cmd.Parameters["@LoginName"].Value = userid;
cn.Open ();
SqlDataReader dr=cmd.ExecuteReader();
if (dr.Read())
{
FormChangePassword mn=new FormChangePassword ();
mn.userid = userid;
mn.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("此用户不存在!");
}
dr.Close();
cn.Close();
}
private void FormLogin_Load(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -