asp.ne bbst源碼分析
1.登陸頁面
1.登陸頁面
登陸頁面中需要注意的大致上就是兩條:如何?驗證碼。如何防止sql注入。
1.如何?驗證碼。
驗證碼簡單的實現可以通過在伺服器端產生一個圖片完成。前台的話添加一個img html控制項,onclick事件中重新載入checkcode頁面。後台中使用dotnet類庫中內建的drawing來實現產生一個圖片。代碼即文檔,代碼如下:
<img id="Img1" alt="看不清,請點擊我。" onclick="this.src=this.src+'?'" src="../ProjectBBS/CheckCode.aspx" mce_src="ProjectBBS/CheckCode.aspx" style="width: 73px; height: 22px" align="left" /></td>
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { CreateCheckCodeImage(GenerateCheckCode()); } private string GenerateCheckCode() { int number; char code; string checkCode = String.Empty; Random random = new Random(); for (int i = 0; i < 4; i++) { number = random.Next(); code = (char)('0' + (char)(number % 10)); checkCode += code.ToString(); } Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); return checkCode; } private void CreateCheckCodeImage(string checkCode) { if (checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics g = Graphics.FromImage(image); try { //產生隨機產生器 Random random = new Random(); //清空圖片背景色 g.Clear(Color.White); //畫圖片的背景雜音線 for (int i = 0; i < 2; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //畫圖片的前景噪音點 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //畫圖片的邊框線 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } }
2.登陸驗證如何去避免sql注入。
微軟的官方網站中有一篇文章詳細介紹防止sql注入:http://msdn.microsoft.com/en-us/library/ff648339.aspx。其中主要內容如下:
2.1輸入驗證,永遠不要相信使用者輸入
using System; using System.Text.RegularExpressions; public void CreateNewUserAccount(string name, string password) { // Check name contains only lower case or upper case letters, // the apostrophe, a dot, or white space. Also check it is // between 1 and 40 characters long if ( !Regex.IsMatch(userIDTxt.Text, @"^[a-zA-Z'./s]{1,40}$")) throw new FormatException("Invalid name format"); // Check password contains at least one digit, one lower case // letter, one uppercase letter, and is between 8 and 10 // characters long if ( !Regex.IsMatch(passwordTxt.Text, @"^(?=.*/d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$" )) throw new FormatException("Invalid password format"); // Perform data access logic (using type safe parameters) ... }
2.2 使用儲存過稱
using System.Data; using System.Data.SqlClient; using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet userDataset = new DataSet(); SqlDataAdapter myCommand = new SqlDataAdapter( "LoginStoredProcedure", connection); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; myCommand.SelectCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11); myCommand.SelectCommand.Parameters["@au_id"].Value = SSN.Text; myCommand.Fill(userDataset); }
在執行過程中,@au_id中傳遞的值是當做純文字的,不會產生sql注入問題。但是這種情況下需要注意的原有的預存程序是如何編寫的。如果是下面的預存程序的話,還是起不到防止注入的功能。
CREATE PROCEDURE dbo.RunQuery @var ntext AS exec sp_executesql @var GO
2.3 使用參數化的動態sql(字串拼接的形式)
using System.Data; using System.Data.SqlClient; using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet userDataset = new DataSet(); SqlDataAdapter myDataAdapter = new SqlDataAdapter( "SELECT au_lname, au_fname FROM Authors WHERE au_id = @au_id", connection); myCommand.SelectCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11); myCommand.SelectCommand.Parameters["@au_id"].Value = SSN.Text; myDataAdapter.Fill(userDataset); }
2.4 資料庫許可權
如果是查詢資料庫的話,將使用權限設定的低一點