简体   繁体   中英

ASP.NET C# Login Redirect

I currently have designed a ASP.NET website which has a login page as its home.

At present I'm using a response.redirect command to forward valid logins to a specific page.

What I would like to do is forward the user to their own page?

I have setup the user database with a Client_ID with the hope of using this to redirect to the page.

eg Client_ID1.aspx would forward login users with a Client_ID number of 1. Client_ID2.aspx would forward login users with a Client_ID number of 2. Client_ID3.aspx would forward login users with a Client_ID number of 3.

My issue is how do I write the response.redirect page with a variable on the end. The code would need to get the Client_ID number from the database and then redirect.

The setup of users is done by me on an admin page therefore I control the setup of the Client_ID part not the user.

This is my code I have when the user clicks the login button:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
    conn.Open();
    string checkuser = "select count(*) from UserData where UserName='" + TextBoxUserName.Text + "'";
    SqlCommand com = new SqlCommand(checkuser, conn);
    int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
    conn.Close();
    if (temp == 1)
    {
        conn.Open();
        string checkPasswordQuery = "select password from UserData where UserName='" + TextBoxUserName.Text + "'";
        SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
        string password = passComm.ExecuteScalar().ToString().Replace(" ","");
        if(password == TextBoxPassword.Text)
        {
            Session["New"] = TextBoxUserName.Text;
            Response.Write("Password is correct");
            Response.Redirect("Manager.aspx");
        }
        else
        {
            Response.Write("Password is Not correct");
        }

    }
    else
    {
        Response.Write("User Name is Not correct");                
    }
}

} Any help on this please?

You can try the following:

int index = realURL.IndexOf(".");

string url = realURL.Substring(0, index);

Response.Redirect(url + clientId.ToString() + ".aspx");

string client_type = getClientTypeFromDB();
switch (client_type)
{
    case "Type1":
        Response.Redirect("~/client1.aspx");
        break;

    case "Type2":
        Response.Redirect("~/client2.aspx");
        break;

    case "Type3":
        Response.Redirect("~/client3.aspx");
        break;
}

You could also store the page itself in the db, and then you could call Response.Redirect(client_type); and use the string returned directly.

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