Visual C# - 根据用户角色将用户重定向到不同的窗体

时间:2014-12-17 01:01:23

标签: c# login user-roles

我想创建一个Login表单,根据用户的角色将用户登录重定向到两个不同的表单。我有两种形式1. UserPanelFrm和2.FrmUserRole以及两个用户角色。 1.管理员和2.User。我想将Admin重定向到UserPanelFrm和User以形成FrmUserRole。我研究了这个过程,但只能为ASP.NET找到有用的资源。

tbl_Staff:

CREATE TABLE [dbo].[tbl_Staff](
[StaffID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Address] [nvarchar](500) NULL,
[Phone] [nvarchar](100) NULL,
[Email] [nvarchar](100) NULL,
[JoinedDate] [date] NULL,
[Username] [nvarchar](50) NULL,
[Password] [nvarchar](max) NULL,
[CreatedDate] [date] NULL,
[Roles] [nvarchar](200) NULL,
[Status] [int] NULL
}

tbl_StaffRoles:

CREATE TABLE [dbo].[tbl_StaffRoles](
[id] [int] NULL,
[RoleDescription] [nvarchar](50) NULL
)

tbl_StaffRoles数据:

id RoleDescription
1 Admin
2 User

嗨,我目前正在使用以下代码进行正常登录。

LoginForm btnLogin:

private void btnLogin_Click(object sender, EventArgs e)
{
    try
    {
        int result = uc.Login(txtUserName.Text, txtPassword.Text);
        if (result == 1)
        {
            this.Hide();
            UserPanelFrm frm = new UserPanelFrm();
            frm.ShowDialog();
            this.Close();
        }
        else
        {
            MessageBox.Show("INVALID USERNAME OR PASSWORD");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

UserClass.cs登录类:

public int Login(String Username, String Password)
{
    try
    {
        int result = 0;
        SqlCommand cmd = new SqlCommand("Select * from tbl_Staff where Username=@Username and Password=@Password", conn);
        cmd.Parameters.AddWithValue("@Username", Username);
        cmd.Parameters.AddWithValue("@Password", Password);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        conn.Close();
        if (dt.Rows.Count > 0)
            result = 1;
        else
            result = 0;

        return result;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

请帮助修改代码,以便我可以根据用户的角色将用户重定向到Windows窗体。

2 个答案:

答案 0 :(得分:3)

修改Login方法以返回数据表(假设您在该查询中有一个角色列):

private void btnLogin_Click(object sender, EventArgs e)
{
    try
    {
        DataTable result = uc.Login(txtUserName.Text, txtPassword.Text);
        if (result.Rows.Count == 1)
        {
            this.Hide();

            string role = result.Rows[0]["Role"].ToString();


            switch (role)
            {
                case "User": 
                    UserPanelFrm frm = new UserPanelFrm();
                    frm.ShowDialog();
                    this.Close();
                    break;
                case "Admin":
                    //Show a different form
                    FrmUserRole fur = new FrmUserRole();
                    fur.ShowDialog();
                    this.Close();
                    break;
            }
        }
        else
        {
        MessageBox.Show("INVALID USERNAME OR PASSWORD");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

和...

public DataTable Login(String Username, String Password)
{
    try
    {
        SqlCommand cmd = new SqlCommand("Select * from tbl_Staff where Username=@Username and Password=@Password", conn);
        cmd.Parameters.AddWithValue("@Username", Username);
        cmd.Parameters.AddWithValue("@Password", Password);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        conn.Close();
        return dt;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

答案 1 :(得分:0)

你应该检查tbl_Staff中的“角色”:

 public int Login(String Username, String Password)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("Select * from tbl_Staff where Username=@Username and Password=@Password", conn);
                cmd.Parameters.AddWithValue("@Username", Username);
                cmd.Parameters.AddWithValue("@Password", Password);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                conn.Close();
                if (dt.Rows.Count > 0)
                {
                    string role = dt.Rows[0]["Roles"].ToString();

                    if (role.Equals("User"))
                    {
                        UserPanelFrm frm = new UserPanelFrm();
                        frm.ShowDialog();
                        this.Close();
                        return 1;
                    }
                    else if (role.Equals("Admin"))
                    {
                        FrmUserRole fur = new FrmUserRole();
                        fur.ShowDialog();
                        this.Close();
                        return 1;
                    }
                }

                return 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                int result = uc.Login(txtUserName.Text, txtPassword.Text);
                if (result == 0)
                {
                    MessageBox.Show("INVALID USERNAME OR PASSWORD");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }