C# SQL查询后重定向页面
假设我处于启用状态,并键入登录信息,以下是每个场景中发生的情况:C# SQL查询后重定向页面,c#,asp.net,sql-server,C#,Asp.net,Sql Server,假设我处于启用状态,并键入登录信息,以下是每个场景中发生的情况: 如果密码错误(或正确)+登录名不存在=>刷新页面 如果密码错误+登录名存在(正确)=>转到 错误\u页面 如果两者都正确=>重定向到CP.aspx 2.和3.都在正常工作,但第一个没有。我不明白为什么它只刷新页面,而不去错误页面,因为细节是错误的。(因为它无法进行比较) 编辑:我还检查了密码是否为空=>重定向到错误页面,但这也会刷新页面。您只需要检查它是否有任何值 if(sqlreader.HasRows) { While
编辑:我还检查了密码是否为空=>重定向到错误页面,但这也会刷新页面。您只需要检查它是否有任何值
if(sqlreader.HasRows)
{
While...
}
else
{
//refresh page code here
}
另外,您可能需要考虑一个更好的架构师n-tie、表示层、业务层和数据访问层。您只需要检查它是否有任何价值
if(sqlreader.HasRows)
{
While...
}
else
{
//refresh page code here
}
另外,您可能需要考虑一个更好的架构师n-tie、表示层、业务层和数据访问层。在链接之前尝试使用
~
:
base.Response.Redirect("~/CP.aspx");
尝试在链接之前使用
~
:
base.Response.Redirect("~/CP.aspx");
技术上:1。如果
sqlreader
没有任何行或2。如果while循环
中没有任何条件获得true
,则将呈现相同的页面。但是要获取页面的引用,可以使用Response.Redirect(Request.RawUrl)
。我喜欢对您的代码进行一些更改—只是一点点:
// since you are selecting 1 top row, here you would 0 or 1 row at all.
// so you don't need to use `while` and `if` doing well
// also, you won't need to check if `sqlreader.HasRows`,
// because the `if (sqlreader.Read())` does the same.
if (sqlreader.Read())
{
if (sqlreader["password"].ToString() == Functions.CreateMD5Hash("5487" + str2.ToString()))
{
this.Session["logged_in"] = "true";
this.Session["username"] = text;
base.Response.Redirect("/CP.aspx");
}
else
{
base.Response.Redirect("/error_page?err=login-fail");
}
}
// there is no need to use an `else`. just do the redirect:
Response.Redirect(Request.RawUrl);
技术上:1。如果
sqlreader
没有任何行或2。如果while循环
中没有任何条件获得true
,则将呈现相同的页面。但是要获取页面的引用,可以使用Response.Redirect(Request.RawUrl)
。我喜欢对您的代码进行一些更改—只是一点点:
// since you are selecting 1 top row, here you would 0 or 1 row at all.
// so you don't need to use `while` and `if` doing well
// also, you won't need to check if `sqlreader.HasRows`,
// because the `if (sqlreader.Read())` does the same.
if (sqlreader.Read())
{
if (sqlreader["password"].ToString() == Functions.CreateMD5Hash("5487" + str2.ToString()))
{
this.Session["logged_in"] = "true";
this.Session["username"] = text;
base.Response.Redirect("/CP.aspx");
}
else
{
base.Response.Redirect("/error_page?err=login-fail");
}
}
// there is no need to use an `else`. just do the redirect:
Response.Redirect(Request.RawUrl);
我已经修改了你的代码,并检查它这将解决你的问题
public static string ReplaceAll(String Str)
{
Str = Str.Replace("'", " ");
Str = Str.Replace(";", " ");
Str = Str.TrimStart();
Str = Str.TrimEnd();
return Str;
}
public void MyFunction()
{
SqlConnection sqlcon = new SqlConnection(Functions.Auth());
SqlDataAdapter sda = new SqlDataAdapter("SELECT TOP 1 password FROM dbo.Accounts WHERE login_name = '" + ReplaceAll(username_login.Text) + "' and password='" + ReplaceAll(password_login.Text) + "'", sqlcon);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
this.Session["logged_in"] = "true";
this.Session["username"] = text;
base.Response.Redirect("/CP.aspx");
}
else
{
base.Response.Redirect("/error_page?err=login-fail");
}}
我已经修改了你的代码,并检查它这将解决你的问题
public static string ReplaceAll(String Str)
{
Str = Str.Replace("'", " ");
Str = Str.Replace(";", " ");
Str = Str.TrimStart();
Str = Str.TrimEnd();
return Str;
}
public void MyFunction()
{
SqlConnection sqlcon = new SqlConnection(Functions.Auth());
SqlDataAdapter sda = new SqlDataAdapter("SELECT TOP 1 password FROM dbo.Accounts WHERE login_name = '" + ReplaceAll(username_login.Text) + "' and password='" + ReplaceAll(password_login.Text) + "'", sqlcon);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
this.Session["logged_in"] = "true";
this.Session["username"] = text;
base.Response.Redirect("/CP.aspx");
}
else
{
base.Response.Redirect("/error_page?err=login-fail");
}}
当登录名不存在时,执行将不会进入while循环(因为sqlreader.Read()将返回false),因此它只呈现相同的页面again@Thangadurai我通过在else语句之后检查Sqlreader.Read()是否为false来修复它。谢谢当登录名不存在时,执行将不会进入while循环(因为sqlreader.Read()将返回false),因此它只呈现相同的页面again@Thangadurai我通过在else语句之后检查Sqlreader.Read()是否为false来修复它。谢谢为了保护应用程序不受SQL注入的影响,请不要将查询与值连接起来,而是使用SqlParameterCollection。我使用ReplaceAll()来避免SQL注入,我不建议这样做,因为您没有涵盖所有不同类型的SQL注入。创建SqlParameterCollection的原因之一是为了防止SQL注入。为了保护应用程序不受SQL注入的影响,请不要使用SqlParameterCollection将查询与值连接起来。我使用ReplaceAll()来避免SQL注入我不建议这样做,因为您没有涵盖所有不同类型的SQL注入。创建SqlParameterCollection的原因之一是为了防止SQL注入。