⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmlogin.cs

📁 稿件名称:基于Visual C# 2005的自定义登录验证框的实现 源代码名称:source code 调试环境:Win XP;.Net
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Threading;
using System.Security.Principal;

public class frmLogin: System.Windows.Forms.Form {

	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main() 
	{
		Application.Run(new frmLogin());
	}


#region " Windows Form Designer generated code "

    public frmLogin() 
	{

        //This call is required by the Windows Form Designer.

        InitializeComponent();

        //Add any initialization after the InitializeComponent() call

    }

    //Form overrides dispose to clean up the component list.

    protected override void Dispose(bool disposing) {

        if (disposing) {

            if (components != null) {

                components.Dispose();

            }

        }

        base.Dispose(disposing);

    }

    //Required by the Windows Form Designer

    private System.ComponentModel.IContainer components = null;

    //NOTE: The following procedure is required by the Windows Form Designer

    //It can be modified using the Windows Form Designer.  

    //Do not modify it using the code editor.
    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.Button btnCancel;
    private System.Windows.Forms.CheckBox chkAdministratorAccount;
    private System.Windows.Forms.Button btnOK;

    private void InitializeComponent() {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmLogin));
        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.btnOK = new System.Windows.Forms.Button();
        this.btnCancel = new System.Windows.Forms.Button();
        this.chkAdministratorAccount = new System.Windows.Forms.CheckBox();
        this.SuspendLayout();
        // 
        // Label1
        // 
        resources.ApplyResources(this.Label1, "Label1");
        this.Label1.Name = "Label1";
        // 
        // Label2
        // 
        resources.ApplyResources(this.Label2, "Label2");
        this.Label2.Name = "Label2";
        // 
        // txtUserName
        // 
        resources.ApplyResources(this.txtUserName, "txtUserName");
        this.txtUserName.Name = "txtUserName";
        // 
        // txtPassword
        // 
        resources.ApplyResources(this.txtPassword, "txtPassword");
        this.txtPassword.Name = "txtPassword";
        // 
        // btnOK
        // 
        resources.ApplyResources(this.btnOK, "btnOK");
        this.btnOK.Name = "btnOK";
        this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
        // 
        // btnCancel
        // 
        resources.ApplyResources(this.btnCancel, "btnCancel");
        this.btnCancel.Name = "btnCancel";
        this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
        // 
        // chkAdministratorAccount
        // 
        resources.ApplyResources(this.chkAdministratorAccount, "chkAdministratorAccount");
        this.chkAdministratorAccount.Name = "chkAdministratorAccount";
        // 
        // frmLogin
        // 
        this.AcceptButton = this.btnOK;
        resources.ApplyResources(this, "$this");
        this.Controls.Add(this.chkAdministratorAccount);
        this.Controls.Add(this.btnCancel);
        this.Controls.Add(this.btnOK);
        this.Controls.Add(this.txtPassword);
        this.Controls.Add(this.txtUserName);
        this.Controls.Add(this.Label2);
        this.Controls.Add(this.Label1);
        this.Name = "frmLogin";
        this.ResumeLayout(false);
        this.PerformLayout();

	}

#endregion

    int intLoginAttempts;


    private void btnCancel_Click(object sender, System.EventArgs e) 
	{
        Application.Exit();

    }

    private void btnOK_Click(object sender, System.EventArgs e) 
	{
        // 创建一个User类型的对象

        Users objUser = new Users();
        GenericPrincipal GenPrincipal;
        string strName = txtUserName.Text;
        string strPassword = txtPassword.Text;

        //依据复选框是否选中而采取相应的验证策略,选中采用基于角色的安全性中Windwos验证策略


		if (chkAdministratorAccount.Checked) 
		{

			if (objUser.IsAdministrator()) 
			{

				MessageBox.Show(Thread.CurrentPrincipal.Identity.Name + 
					" 欢迎进入**系统!","登录成功",
					MessageBoxButtons.OK, MessageBoxIcon.Information);

                //如果为管理员角色,则显示后台后台账号数据界面 
                frmMain Main = new frmMain();
				Main.ShowDialog();

				// Hide the Login Form

				this.Close();
			}
			else 
			{

                //通过intLoginAttempts来判断用户尝试登录次数,如果大于3次则程序强行退出

				intLoginAttempts += 1;

				MessageBox.Show("User not an Administrator.  Please provide a User Name and Password.", this.Text,
					MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

			}
		}
		else 
		{

            // 判断输入的帐户是否在已注册帐户之列

			if (objUser.IsLogin(strName, strPassword)) 
			{
				GenPrincipal = objUser.GetLogin(strName, strPassword);
				Thread.CurrentPrincipal = GenPrincipal;

				

				MessageBox.Show(Thread.CurrentPrincipal.Identity.Name +
                " 欢迎进入**系统!", "登录成功",
					MessageBoxButtons.OK,MessageBoxIcon.Information);

                //如果为已注册帐户,则显示后台后台账号数据界面
				frmMain Main = new frmMain();

				Main.ShowDialog();

                // 将登录界面隐藏

				this.Close();
			}
			else 
			{
                //通过intLoginAttempts来判断用户尝试登录次数,如果大于3次则程序强行退出

				intLoginAttempts += 1;
				
				if (intLoginAttempts >= 3) 
				{

					MessageBox.Show("多次输入错误,请稍候重试!",this.Text,
						MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
					Application.Exit();
				}
				else 
				{

                    MessageBox.Show("无法找到该用户,请重试!", this.Text,
							MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

				}

				}

			}

			}

		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -