我具有以下的認證方法:防止SQL注入在asp.net
protected void Button1_Click(object sender, EventArgs e)
{
string s;
s = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(s);
con.Open();
string sqlCmd;
sqlCmd = "SELECT Username, UserPassword FROM Benutzer WHERE Username = @Username AND UserPassword [email protected]";
SqlCommand cmd = new SqlCommand(sqlCmd, con);
String username = tbUsername.Text.Replace("'", "''");
String password = tbPassword.Text.Replace("'", "''");
cmd.Parameters.AddWithValue("Username", username);
cmd.Parameters.AddWithValue("Password", password);
string CurrentName;
CurrentName = (string)cmd.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = cmd.Parameters[0].ToString();
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Benuztername/Password ungültig!";
}
}
是這足以防止SQL注入?我以前只是用戶名和密碼,直接進入命令是這樣的:
sqlCmd = "SELECT Username, UserPassword FROM Benutzer WHERE Username ='" + username + "' AND UserPassword ='" + pwd + "'";
其中username和pwd那裏只是字符串變量中的用戶名和密碼文本框的內容保存...
編輯:
確定我已經編輯我的代碼現在看起來是這樣的:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection objcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlDataAdapter objda = new SqlDataAdapter("[MembershipPruefen]", objcon);
objda.SelectCommand.CommandType = CommandType.StoredProcedure;
objda.SelectCommand.Parameters.Add("@Username", SqlDbType.VarChar).Value = tbUsername.Text;
objda.SelectCommand.Parameters.Add("@UserPassword", SqlDbType.VarChar).Value = tbPassword.Text;
objcon.Open();
string CurrentName;
CurrentName = (string)objda.SelectCommand.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = tbUsername.Text;
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Benuztername/Password ungültig!";
}
objcon.Close();
}
這是我的存儲過程:
CREATE PROCEDURE MembershipPruefen (@Username VARCHAR(50), @UserPassword VARCHAR(50))
AS
SELECT Username, UserPassword FROM Benutzer WHERE Username LIKE @Username AND UserPassword LIKE @UserPassword;
這就夠了嗎?我的網絡應用程序是否可以安全地防止SQL入侵或者還有什麼可做的事情?
我可以知道爲什麼你只是不使用存儲過程和多層技術?它真的有幫助 – Marwan 2013-03-12 14:16:59
,因爲我對此很新,而且我不知道從哪裏開始......我的時間有點有限,所以我必須儘快完成這項工作......我閱讀了很多文章,消毒,參數化和存儲過程通常是安全的,以防SQL注入,所以我做了第一個2,但我不知道它們是否足夠... – LeonidasFett 2013-03-12 14:23:19
對你有好處,你跳過字符串連接! – Michel 2013-03-12 15:08:49