I am getting browser error: This page isn't working localhost redirected too many times

Donald Symmons 2,856 Reputation points
2023-03-01T11:16:55.71+00:00

I have this login code which I use to login any user based on their roles. An admin is redirected to the pages in the Admin folder, while other users are redirected to pages in the root directory. This was working fine for a very long time. Suddenly, it decided to give me error when I tried to login as an admin. However, when I login as normal user, the redirection is okay; it redirects normal user to the user pages in the root directory.

ERR

Here is my login code

 protected void ValidateUser(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUsername.Text) & !string.IsNullOrEmpty(txtPassword.Text))
            {
                string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT Uid, RoleId FROM Users WHERE email = @email AND pass = @pass", con))
                    {
                        con.Open();
                        cmd.Parameters.AddWithValue("@email", txtUsername.Text.Trim());
                        cmd.Parameters.AddWithValue("@pass", Encrypt(txtPassword.Text.Trim()));
                        //string Id = Convert.ToString(cmd.ExecuteScalar());
                        SqlDataReader sdr = cmd.ExecuteReader();
                        string Id = string.Empty, RoleId = string.Empty;
                        if (sdr.Read())
                        {
                            Id = Convert.ToString(sdr["Uid"]);
                            RoleId = Convert.ToString(sdr["RoleId"]);
                        }
                        con.Close();

                        if (!string.IsNullOrEmpty(Id))
                        {
                            string users = "";
                            using (SqlCommand cmd1 = new SqlCommand("SELECT Uid FROM UserActivation WHERE Uid = @Uid"))
                            {
                                cmd1.CommandType = CommandType.Text;
                                cmd1.Parameters.AddWithValue("@Uid", Id);
                                cmd1.Connection = con;
                                con.Open();
                                users = Convert.ToString(cmd1.ExecuteScalar());
                                con.Close();
                            }
                            if (string.IsNullOrEmpty(users))
                            {
                                int user = 0;
                                using (SqlCommand cmd2 = new SqlCommand("SELECT Uid FROM Users WHERE pass = @pass COLLATE SQL_Latin1_General_CP1_CS_AS AND email = @email AND pass = @pass"))
                                {
                                    cmd2.CommandType = CommandType.Text;
                                    cmd2.Parameters.AddWithValue("@email", txtUsername.Text.Trim());
                                    cmd2.Parameters.AddWithValue("@pass", Encrypt(txtPassword.Text.Trim()));
                                    cmd2.Connection = con;
                                    con.Open();
                                    user = Convert.ToInt32(cmd2.ExecuteScalar());
                                    con.Close();
                                }
                                if (user > 0)
                                {
                                    Session["user"] = Id;
                                    con.Open();
                                    string query = "SELECT Suspend from Users WHERE Uid = @Uid";
                                    using (SqlCommand cmd3 = new SqlCommand(query, con))
                                    {
                                        cmd3.Parameters.AddWithValue("@Uid", Session["user"]);
                                        DataTable dtb = new DataTable();
                                        SqlDataAdapter da = new SqlDataAdapter(cmd3);
                                        da.Fill(dtb);
                                        string suspend = dtb.Rows[0]["Suspend"].ToString().Trim().ToLower();
                                        if (suspend == "0")
                                        {
                                            string UpdateLog = @"UPDATE Users SET LastLogin=@dateandtime, IsActive=@IsActive WHERE Uid = @Uid";
                                            using (SqlCommand cmd4 = new SqlCommand(UpdateLog, con))
                                            {
                                                cmd4.Parameters.AddWithValue("@dateandtime", DateTime.UtcNow);
                                                cmd4.Parameters.AddWithValue("@IsActive", "1");
                                                cmd4.Parameters.AddWithValue("@Uid", Session["user"]);
                                                cmd4.ExecuteNonQuery();
                                                con.Close();
                                            }
                                            SqlCommand cmd5 = new SqlCommand("SELECT RoleName From [RoleTable] WHERE RoleId = @RoleId", con);
                                            con.Open();
                                            cmd5.Parameters.AddWithValue("@RoleId", RoleId);
                                            DataTable dt = new DataTable();
                                            SqlDataAdapter sda = new SqlDataAdapter(cmd5);
                                            sda.Fill(dt);
                                            if (dt.Rows.Count > 0)
                                            {
                                                string roles = dt.Rows[0]["RoleName"].ToString().Trim().ToLower();
                                                if (roles == "superadmin")
                                                {
                                                    Session["user"] = Id;
                                                    FormsAuthentication.RedirectFromLoginPage(Id, true);
                                                    Response.Redirect("~/Admin/admindashboard.aspx");
                                                }
                                                else if (roles == "admin")
                                                {
                                                    Session["user"] = Id;
                                                    FormsAuthentication.RedirectFromLoginPage(Id, true);
                                                    Response.Redirect("~/Admin/admindashboard.aspx");
                                                }
                                                else if (roles == "superuser")
                                                {
                                                    Session["user"] = Id;
                                                    FormsAuthentication.RedirectFromLoginPage(Id, true);
                                                    Response.Redirect("Overview.aspx");
                                                }
                                                else if (roles == "user")
                                                {
                                                    Session["user"] = Id;
                                                    FormsAuthentication.RedirectFromLoginPage(Id, true);
                                                    Response.Redirect("Overview.aspx");
                                                }
                                                else
                                                {
                                                    Response.Redirect("Login.aspx");
                                                }
                                            }
                                        }
                                        else
                                        {
                                            dvMessage.Visible = true;
                                            lblMessage.Visible = true;
                                            lblMessage.ForeColor = System.Drawing.Color.Red;
                                            lblMessage.Text = "Account has been Temporary Suspended";
                                        }
                                    }

                                }
                            }
                            else
                            {
                                dvMessage.Visible = true;
                                lblMessage.Visible = true;
                                lblMessage.ForeColor = System.Drawing.Color.Red;
                                lblMessage.Text = "Account has not been activated";
                                txtPassword.Text = "";
                                txtPassword.Focus();
                            }
                        }
                        else
                        {
                            dvMessage.Visible = true;
                            lblMessage.Visible = true;
                            lblMessage.ForeColor = System.Drawing.Color.Red;
                            lblMessage.Text = "Invalid Login Details";
                            txtPassword.Text = "";
                            txtPassword.Focus();
                        }
                    }
                }
            }
            else
            {
                dvMessage.Visible = true;
                lblMessage.Visible = true;
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "All Fields are Required";
            }
        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,251 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zsolt Hajdu 156 Reputation points
    2023-03-04T16:30:11.49+00:00

    In the provided code, I cannot find any explicit redirect statements. However, there are several Response.Redirect statements that could cause the redirection loop if the conditions are not properly handled.

    To troubleshoot the issue, you can try commenting out the Response.Redirect statements and see if the error still occurs. If the error goes away, then you can narrow down the issue by uncommenting each redirect statement one by one and verifying that it works as expected.

    You can also try to debug the code by setting breakpoints and stepping through the code to see where the redirection loop is occurring.

    Additionally, you should check the configuration settings of your web application to ensure that there are no conflicting settings that could cause the redirection loop. For example, check the web.config file to see if there are any conflicting authentication settings or URL rewrite rules.

    Finally, make sure that the URL being redirected to is valid and accessible. If the URL is incorrect or the page does not exist, it could cause the redirection loop.


0 additional answers

Sort by: Most helpful