我想用我的 ASP.net 代码完成两件事,因为我是初学者,所以我现在正在努力完成:
1) 我在Login.aspx中的代码目前容易受到 SQL 注入的影响,如何才能将我的 SQL 注入风险降到最低?
using System;
using System.Collections;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
public partial class login : System.Web.UI.Page
{
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login_Box.UserName;
string pwd = Login_Box.Password;
string strConn;
strConn = WebConfigurationManager.ConnectionStrings["team13ConnectionString"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
string sqlUserName;
sqlUserName = "SELECT id,pass FROM ts_dept ";
sqlUserName += " WHERE (id ='" + username + "')";
sqlUserName += " AND (pass ='" + pwd + "')";
SqlCommand com = new SqlCommand(sqlUserName, Conn);
string CurrentName;
CurrentName = (string)com.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else
{
Session["UserAuthentication"] = "";
}
}
}
2) Default.aspx是我网站的主页。如果用户尚未登录,则无法访问它,我如何在此处检查 Session["Authentication"] (如果为 null,则用户被重定向到Login.aspx。这是我目前的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Team13
{
public partial class default : System.Web.UI.Page
{
}
}