简体   繁体   中英

How to redirect to a specific layout page according to user type?

I have a login page that currently redirects to a default layout page when a user successfully logs in. However, I have created a different layout page for the different user and what I want to do is to be able to look through my database and check the type of user and when their username and password is correct, response.redirect them to a specific layout page depending on what type of user they are. I have different views for each user type.

The different user views under shared folder.

public partial class Login : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    SqlConnection con = new SqlConnection();
    SqlDataAdapter sda = new SqlDataAdapter();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = "Data Source=CHRIS\\SQLEXPRESS;Initial Catalog=FPSDD;Integrated Security=True";
        con.Open();
    }

    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        cmd.CommandText = "SELECT PersonType FROM Person where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and PersonType='" + userType.SelectedValue + "'";
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(ds, "Person");
        if (ds.Tables[0].Rows.Count > 0)
        {
            if (userType.SelectedValue == "Student")
            {
                Response.Redirect(Url.Action(""));
            }
            else if (userType.SelectedValue == "Instructor")
            {
                Response.Redirect("");
            }
            else if (userType.SelectedValue == "Counselor")
            {
                Response.Redirect("");
            }
            else if (userType.SelectedValue == "Parent")
            {
                Response.Redirect("");
            }
            else if (userType.SelectedValue == "Principal")
            {
                Response.Redirect("");
            }
            else if (userType.SelectedValue == "Admin")
            {
                Response.Redirect("");
            }
            else
            {
                cmd.CommandText = "SELECT PersonType FROM Person where Username='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "'";
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(ds, "Person");

                if (ds.Tables[0].Rows.Count > 0)
                {
                    Label1.Text = "Invalid User Type. Please Try Again!";
                }
                else
                {
                    Label1.Text = "Invalid User Type, Username or Password. Please Try Again!";
                }
            }
        }
    }

I will expand on MCoder's original answer.

If you utilize:

return RedirectToAction("YourMethodName", "YourControllerName");

You can pass an overload of this Redirect method a parameter.

When your user logs in, return his/her user type. Pass it into the Redirect like so:

return RedirectToAction("YourMethodName", "YourControllerName", new { paramName = userType });

And in YourController.YourMethod(paramType paramName) have a conditional statement.

if(paramName = x)
{
    return View("CorrectViewName", appropriateViewModel); 
}
else ...

If you have more than 2 or 3 user types, you can use Switch/Case instead of If/Else.

Assuming this is a MVC web application. Do not use response.redirect()

instead use

return RedirectToAction("YourMethodName", "YourControllerName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM