1

我正在使用 Visual Studio 2010 和 C# 中的 Windows 窗体应用程序。该应用程序正常运行我可以完美登录,并将用户名登录到下一个表单,这是登录成功后打开的菜单表单。现在我的问题是我想检查登录的用户是经理还是管理员,这取决于存储在数据库中角色列中的值,所以我可以禁用管理员用户的某些功能。有什么想法或帮助将我引导到正确的地方吗?我希望我的问题很清楚

我有一个名为 tblUsers 的表,其中包含以下字段:

userId
Username
Password
FirstName
LastName
Mobile
Landline
Address
Email
Role

//clsLoginCollection:

     public Boolean Login(clsLogin Login)
      {
        clsDataConduit logincheck = new clsDataConduit();
        logincheck.AddParameter("Username", Login.username);
        logincheck.AddParameter("Password", Login.password);

        logincheck.Execute("sproc_tblUser_Login");

        if (logincheck.Count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

//存储过程:

ALTER PROCEDURE sproc_tblUser_Login
@Username varchar(10),
@Password varchar(10)

AS

select Username, Password from tblUsers where Username = @Username AND Password = @Password

//clsLoginform:

    private void btnLogin_Click(object sender, EventArgs e)
    {
       Boolean b = false;

        if ((String.IsNullOrEmpty(tbUsername.Text)) || (String.IsNullOrEmpty(tbPassword.Text)))
        {
            MessageBox.Show("Username or Password Cannot Be Blank.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            clsLoginCollection l = new clsLoginCollection();
            clsEncryption encrypt = new clsEncryption();// this class have the encryption method
            b = l.Login(new clsLogin(tbUsername.Text, encrypt.Encrypt(tbPassword.Text)));

            if (b == true)
            {
                MainMenu m = new MainMenu(tbUsername.Text, tbPassword.Text);
                m.Show();
                this.Visible = false;
            }
            else
            {
                MessageBox.Show("Invalid Login Details", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
            }
        }
    }

//cls 完整登录

public class clsLogin
{
    private Int32 UserID;
    private string Username;
    private string Password;
    private string FirstName;
    private string LastName;
    private string Mobile;
    private string Landline;
    private string Address;
    private string Email;
    private string Role;

    public clsLogin()
    { }

    public clsLogin(string UserName, string Password)
    {
        this.username = UserName;
        this.Password = Password;
    }

    public clsLogin(string UserName, string Password, string FirstName, string LastName, string Mobile, string Landline, string Address, string Email, string Role)
    {
        this.username = UserName;
        this.Password = Password;
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Mobile = Mobile;
        this.Landline = Landline;
        this.Address = Address;
        this.Email = Email;
        this.Role = Role;
    }

    public Int32 userID
    {
        get { return UserID; }
        set { UserID = value; }
    }

    public string username
    {
        get {return Username;}
        set {Username = value;}
    }

    public string password
    {
        get {return Password;}
        set {Password = value;}
    }

    public string firstName
    {
        get { return FirstName; }
        set { FirstName = value; }
    }

    public string lastName
    {
        get { return LastName; }
        set { LastName = value; }
    }

    public string mobile
    {
        get { return Mobile; }
        set { Mobile = value; }
    }

    public string landline
    {
        get { return Landline; }
        set { Landline = value; }
    }

    public string address
    {
        get { return Address; }
        set { Address = value; }
    }

    public string email
    {
        get { return Email; }
        set { Email = value; }
    }

    public string role
    {
        get { return Role; }
        set { Role = value; }
    }
}
4

2 回答 2

0

像这样的东西?

SELECT Role FROM tblUsers WHERE Username = @Username
于 2013-04-10T22:59:04.110 回答
0

这是我对我提出的问题的回答。我使用了 rikki b 提供的查询

//clsLoginCollection(新增返回角色的方法)

public string CheckRole(clsLogin Rolecheck)
    {
        string Role;
        clsDataConduit dataconduit = new clsDataConduit();
        dataconduit.AddParameter("@Username", Rolecheck.username);
        dataconduit.AddParameter("@Password", Rolecheck.password);
        dataconduit.Execute("sproc_tblUsers_FilterByRole");

        Role = dataconduit.QueryResults.Rows[0]["Role"].ToString();

        return Role;
    }

在我的 Login.cs 类上,我创建了一个字符串变量来保存此方法的结果

string role = ""

和下面

logindetails = l.Login(new clsLogin(tbUsername.Text, encrypt.Encrypt(tbPassword.Text))); 

我已经添加了这段代码

roleofuser =  l.CheckRole(new clsLogin(tbUsername.Text, encrypt.Encrypt(tbPassword.Text)));

因此,如果用户是管理员、经理等,则此“roleofuser”将返回,具体取决于角色,然后使用 if/else 语句,我能够将它们重定向到正确的形式

谢谢 以防万一有人陷入这个特殊问题

于 2013-04-14T17:18:21.300 回答