当用户已在不同选项卡中登录同一浏览器时,如何停止在(asp.net webforms)中打开登录页面

时间:2015-12-28 10:14:18

标签: asp.net

我已经以用户身份登录并且工作正常,但是当我尝试在同一浏览器的另一个标签页中打开登录时,它仍然会转到login.aspx而没有实际的成员页面

请帮忙!

Aboutus.aspx

    protected void Page_Load(object sender, EventArgs e)
    {
        if(Session["Username"] == null)

        {
            Response.Redirect("Login.aspx");


        }
        else
        {
            string Username = Session["Username"].ToString();
            Label1.Text = Username;
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("Login.aspx");
    }
}

的Login.aspx

protected void Page_Load(object sender,EventArgs e)         {

    }



    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {



            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select COUNT(*)FROM [dbo].[Reg] WHERE Username='" + Login1.UserName + "' and Password=@pass");
            cmd.Connection = con;
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            ////create an array of bytes we will use to store the encrypted password
            Byte[] hashedBytes;
            ////Create a UTF8Encoding object we will use to convert our password string to a byte array
            UTF8Encoding encoder = new UTF8Encoding();

            ////encrypt the password and store it in the hashedBytes byte array
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Login1.Password));

            cmd.Parameters.AddWithValue("@pass", hashedBytes);
        var username = Login1.UserName;
            int OBJ = Convert.ToInt32(cmd.ExecuteScalar());
            if (OBJ > 0)
            {
            if (username == "admin")
            {
                Session["Username"] = Login1.UserName;
                Response.Redirect("AdminPanel.aspx");
            }
            else
            {
                Session["Username"] = Login1.UserName;
                Response.Redirect("About.aspx");
            }
           }
            else
            {
                Label1.Text = "Invalid username or password";
                this.Label1.ForeColor = Color.Red;
            }


    }
}

}

2 个答案:

答案 0 :(得分:1)

您在登录页面上的页面方法必须像这样

  protected void Page_Load(object sender, EventArgs e)
{
    if(Session["Username"] == null)

    {


    }
    else
    {
        Response.Redirect("index.aspx",false);
    }

}

我希望这会有所帮助

答案 1 :(得分:1)

在Login.aspx的Page_Load中执行此操作:

if(Session["Username"] != null)

    {
        string username = Convert.ToString(Session["Username"]);
        if (username == "admin")
            {

                Response.Redirect("AdminPanel.aspx");
            }
            else
            {

                Response.Redirect("About.aspx");
            }

    }