Asp.net FormsAuthentication.RedirectFromLoginPage不工作

Asp.net FormsAuthentication.RedirectFromLoginPage不工作,asp.net,Asp.net,我正在登录页面。与数据库或C#代码相关的所有内容都正常工作,但成功登录后,我无法重定向到主页。aspx,我是否遗漏了什么?请帮忙。 代码: </authentication> <authorization> <deny users="*"/> </authorization> protected void Button1_Click(object sender, EventArgs e) { string sourc

我正在登录页面。与数据库或C#代码相关的所有内容都正常工作,但成功登录后,我无法重定向到主页。aspx,我是否遗漏了什么?请帮忙。 代码:

</authentication>
<authorization>
  <deny users="*"/>

</authorization>
protected void Button1_Click(object sender, EventArgs e)
    {
        string source = "server=localhost\\sqlexpress;Database=LogInDB;Trusted_Connection=True";
        SqlConnection con = new SqlConnection(source);
        con.Open();
        SqlCommand cmd = new SqlCommand("proc_LogIn", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = TextBox1.Text;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = TextBox2.Text;
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
                    }
        else
        {
            Response.Write("Invalid credentials");
        }

    }
Web.Config:

</authentication>
<authorization>
  <deny users="*"/>

</authorization>
protected void Button1_Click(object sender, EventArgs e)
    {
        string source = "server=localhost\\sqlexpress;Database=LogInDB;Trusted_Connection=True";
        SqlConnection con = new SqlConnection(source);
        con.Open();
        SqlCommand cmd = new SqlCommand("proc_LogIn", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = TextBox1.Text;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = TextBox2.Text;
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
                    }
        else
        {
            Response.Write("Invalid credentials");
        }

    }

</authentication>
<authorization>
  <deny users="*"/>

</authorization>
它应该是

</authentication>
<authorization>
  <deny users="*"/>

</authorization>


除了执行Mladen提到的设置外,在调用该方法之前,您还必须在web.config中执行这些设置。

在我的例子中,尽管在我的web.config中提供了来自这两个答案的代码,
用于身份验证。RedirectFromLoginPage
正在导航回
loginUrl

</authentication>
<authorization>
  <deny users="*"/>

</authorization>
这似乎是这两个线程(,)中描述的一个已知问题。正如上面提到的,使用authCookie和重定向的组合对我来说很有效

</authentication>
<authorization>
  <deny users="*"/>

</authorization>
VB.NET代码:

</authentication>
<authorization>
  <deny users="*"/>

</authorization>
protected void Button1_Click(object sender, EventArgs e)
    {
        string source = "server=localhost\\sqlexpress;Database=LogInDB;Trusted_Connection=True";
        SqlConnection con = new SqlConnection(source);
        con.Open();
        SqlCommand cmd = new SqlCommand("proc_LogIn", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = TextBox1.Text;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = TextBox2.Text;
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
                    }
        else
        {
            Response.Write("Invalid credentials");
        }

    }
FormsAuthentication.SetAuthCookie(txtUsername.Value,False)
重定向(FormsAuthentication.DefaultUrl,False)

我有一个问题,为什么我们需要使用FormsAuthentication.RedirectFromLoginPage方法?为什么Response.Redirect()不起作用。?RedirectFromLoginPage不仅仅是重定向。它会创建一个cookie,并将您重定向到上一页(您单击了登录按钮)。有关更多详细信息,请参阅。