Click here to Skip to main content
14,696,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I dont know how to redirect users on control-panel.aspx page and admin on admin.aspx page.
I have tried this but not working.
Please help

What I have tried:

<pre>  protected void Button1_Click(object sender, EventArgs e)
    {           
        string user = TextBox1.Text.Trim();
        cmd.CommandText = "select* from Users where Email='" + TextBox1.Text + "' and Password= '"+ TextBox2.Text +"'";
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(ds, "Users");
       
        SqlCommand check_User_Name = new SqlCommand("SELECT count([Role]) Users WHERE ([Role] = 'Admin' ");
        check_User_Name.CommandType = CommandType.Text;
        SqlDataAdapter sdaa = new SqlDataAdapter(check_User_Name);
        DataTable dt = new DataTable();      
        sdaa.Fill(dt);

            if (ds.Tables[0].Rows.Count > 0)
            {

                if (dt.Rows.Count > 0)
                {
                    Response.Redirect("Admin.aspx");
                }
                else
                {
                    Session["user"] = user;
                    Response.Redirect("Control-Panel.aspx");
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Something gone wrong');", true);
            }
    }
Posted
Updated 24-Jan-18 4:49am

For starters, don't do it like that! So many things wrong here...
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
DROP TABLE MyTable;
A perfectly valid "delete the table" command
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And doing it on the login page for a website? Where anyone, anywhere can do what they want? That's almost criminally silly.

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] Combine that with teh SQL Injection, and I can log in as who I want, when I want, without even knowinhg a single password...

3) Get your SQL right.
new SqlCommand("SELECT count([Role]) Users WHERE ([Role] = 'Admin' ");
Without a 'FROM' before 'Users' that will cause an SQL exception.

Fix these, in that order as a matter of priority throughout your whole app: or you will lose your database - heck you best mate will try it to see what happens and just to see the look on your face...
   
Comments
Gatsby29 24-Jan-18 8:33am
   
Thank you mate for your concern and long answer. I am just beginner and i am trying to do this app for practice, later i will implement hashing and other stuffs. Just trying to make this basic login work right and i have problem in this if statement or filling data table i am not sure even with good sql command not working.
OriginalGriff 24-Jan-18 8:46am
   
Never, ever do that!
If you are told "this and this are dangerous" do not assume that "it'll be alright for the moment, I'll come back later and fix it". You won't, it's boring and you have more interesting / pressing projects on your plate.
Instead, get into the habit of doing it properly from the beginning. It's a lot, lot quicker that trying to fix things after a major problem caused by your shortcuts. And it's a lot easier to get into "the right habits" early, than it is to try and switch to them later on!
SELECT count([Role]) Users WHERE ([Role] = 'Admin'


This isn't valid, there is no "from" and you aren't closing the last parenthesis

SELECT count([Role]) from Users WHERE ([Role] = 'Admin')


Secondly if you think "Users" in this statement is accessing the "Users" table you created here;

sda.Fill(ds, "Users");


Then it isn't. Regardless if you have a field called "Role" in your users table then you don't need the second command, you already have the data you need in the "Users" table in "ds". Check the count of rows is 1 and if it is read the "Role" field of the first row of the data table and that will give you that user's role.

As OriginalGriff has already pointed out, literally everything you are doing here is bad :) I'll add to his comments by stating that your design means a person can only be in one role, but I appreciate you're just trying to get the basics working.
   
Comments
Gatsby29 24-Jan-18 9:40am
   
Thank you guys it work good now. I just watched many tutorials and in each utotrial they do it different i want to achieve that i have Register and login form with hashed password, mail confirmation, password recovery and save it all in database with date time and login form with roles (admin and users) with option remember me. And after login i want to show user data like name , surname , profile image etc... Cant find anywhere this example so i must combine different tutorials and than from time to time i have problems when i dont know what to do. I have several projects where i test it.
If someone know some good place for good tutorials about my goal please post it, thank you a lot.
Try this

if (ds.Tables[0].Rows.Count > 0)
            {
                DataTable dtUsers = ds.Tables["Users"];
                Session["user"] = user;
                if (dtUsers.Rows.Count == 1) // it should return only one row 
                {
                    if (dtUsers.Rows[0]["Role"].ToString() == "Admin")
                    Response.Redirect("Admin.aspx");
                    else
                        Response.Redirect("Control-Panel.aspx");
                }
                else
                { 
                    Response.Redirect("Control-Panel.aspx");
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Something gone wrong');", true);
            }


Since you have already filled the user information into the dataset, there is no need to query the role once again to check the role, you shall get the role information from the dataset it self.

Note: Concatenating the sql Query string is vulnerable to SQL Injection[^] attacks,always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[[^]

you shall remove these codes
SqlCommand check_User_Name = new SqlCommand("SELECT count([Role]) Users WHERE ([Role] = 'Admin' ");
      check_User_Name.CommandType = CommandType.Text;
      SqlDataAdapter sdaa = new SqlDataAdapter(check_User_Name);
      DataTable dt = new DataTable();
      sdaa.Fill(dt);
   
Comments
Gatsby29 24-Jan-18 10:25am
   
This is exactly what i needed
Karthik_Mahalingam 24-Jan-18 10:26am
   
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900