Click here to Skip to main content
15,914,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
protected void btn_login_Click(object sender, EventArgs e)
        {
            string UserName = TextBox_user_name.Text.Trim();
            string Password = TextBox_password.Text.Trim();
            SqlConnection con = new SqlConnection(strConnString);

            con.Open();
            str = "Select UserName,Password from  Login";
            com = new SqlCommand(str);
            sqlda = new SqlDataAdapter(com.CommandText, con);
            dt = new DataTable();
            sqlda.Fill(dt);
            RowCount = dt.Rows.Count;
            for (int i = 0; i < RowCount; i++)
            {
                UserName = dt.Rows[i]["UserName"].ToString();
                Password = dt.Rows[i]["Password"].ToString();
                if (UserName == TextBox_user_name.Text && Password == TextBox_password.Text)
                {
                    Session["UserName"] = UserName;
                    Response.Redirect("~/Admin/Dashbord.aspx");
                }
                else
                {
                    lb1.Text = "Invalid User Name or Password! Please try again!";
                }
            }
        }

Here my problem is when I login The page goes to /Admin/Dashbord.aspx but when I press The Back button the the page goes back to Login.aspx.
My Second problem is When I put the URL of the pages i.e.~/Admin/Dashbord.aspx then also the page opens how to stop these two problems
Posted

1 solution

solution is depend on what is your requirements on those cases. for example in case of back button click in dashboard you need to reload it back to dashboard what you can do is, in the login page page_load event you can check the Session["UserName"] and if it is exist then redirect to dashboard page.

to restrict direct access to Dashboard page you can check Session["UserName"] in page_load event of Dashboard page and if it is not exist then redirect user to login page.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if(Session["UserName"] ==null)
    {
       Response.Redirect("~/Login.aspx"); // give correct url
    }
}
 
Share this answer
 
v2
Comments
Member 11111143 8-Apr-15 1:41am    
I can now sort out the first problem by myself with the help of javascript function but I am not understanding how to solve the second Problem i.e the url Damith can you tell me some code for this.

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