[來源:J.D. Meier's Blog] 微軟剛推出了一個ASP.NET 2.0 Internet 安全之參考實現( ASP.NET 2.0 Internet Security Reference Implementation)。這是個配有所有編碼和指導性文檔的樣本應用,其宗旨是示範在實際應用中如何應用模式和實踐之安全向導中的最佳實踐。這個應用是從Pet Shop 4發展而來,使之適用於Internet。該應用使用了表單認證,用戶和角色數據是儲存在SQL數據庫裏的。 該應用能夠在其官方網站上下載: ASP_NET 2_0 Internet Security Reference Implementation: Home http://www.gotdotnet.com/codegallery/codegallery.aspx?id=48f35de8-cd92-4ac6-9144-12d5a13f22ff [找不到連接] 下載的內容包括三部分 1。VS 2005方案和編碼 2。Internet 安全參考實現的指導性文檔 3。場景(Scenario)和方案文檔 在安全參考實現的指導性文檔裏,涉及的設計決策包括下述分類 1。認證 2。受權 3。輸入和數據驗證 4。數據訪問 5。異常管理 6。敏感數據(Sensitive Data) 7。審記和日誌記錄(Auditing and Logging) 在每一個分類裏又具體列出了詳細的設計決策,譬如,在認證方面,要作的決定包括 1。使用表單認證 2。使用SQL成員提供器 3。使用SSL來保護身份驗證信息和認證cookies 4。不直接存儲明文密碼 5。強制使用安全性強的密碼 6。保護對身份驗證信息存儲的訪問 7。不除久認證cookies 8。在認證cookies上設置HttpOnly 9。使用獨特的cookie名字和路徑 對每個決定,又詳細列出 1。是怎麼實現的 2。這麼作的緣由 3。好處 4。缺點 5。相關資源 涉及的方面不少,內容很是全,是一個學習設計/實現安全Web應用的好範例 Asp.Net安全驗證小結 1,基於windows的安全驗證 web.config文件: configuration system.web authentication mode="Windows" / identity impersonate="true" / authorization allow roles="BUILTIN\groupname" users="computername\UserName,computername\UserName" / deny users="*" / /authorization /system.web /configuration 在.aspx文件中無需任何代碼就能夠實現驗證,但能夠在.aspx文件獲取登錄用戶的信息 需導入命名空間:System.Security.Principal if(User.Identity.IsAuthenticated)//判斷用戶是否驗證,彷佛無關緊要 { WindowsIdentity objWinIdentity=WindowsIdentity.GetCurrent(); lblHelloMsg.Text="the name:"+objWinIdentity.Name+"brType:"+ objWinIdentity.AuthenticationType+"IsInRole:"+User.IsInRole("computername\\groupname"); } 2,基於web.config forms驗證 web.config文件: configuration system.web authentication mode="Forms" forms name="MyApp" path="/" loginUrl="login.aspx" protection="All" timeout="30" credentials passwordFormat="Clear" user name="kwk" password="test" / user name="ljx" password="test" / /credentials /forms /authentication authorization allow users="kwk,ljx" / deny users="?" / /authorization /system.web /configuration login.aspx文件:須要提供兩個文本框用於填寫用戶和密碼(txtUsr,txtPwd),一個單選框判斷是否永久保存 還須要一個按鈕控件則響應該button的代碼以下: void DoLogin(Object sender, EventArgs e) { if(FormsAuthentication.Authenticate(txtUsr.Value,txtPwd.Value)) { FormsAuthentication.RedirectFromLoginPage(txtUsr.Value,chkPersist.Checked); } else //爲代碼完整性而設置,能夠不寫 { Response.Write("authentication fails"); } } 而後在別的頁面能夠得到登錄用戶的值: if(User.Identity.IsAuthenticated)//能夠不須要判斷 { Response.Write("your name:"+User.Identity.Name); Response.Write("驗證類型:"+User.Identity.AuthenticationType);//forms,windows等 } 3,基於自定義forms驗證 web.config文件(基本上不須要什麼設置): system.web authentication mode="Forms" forms name="MyApp" path="/" loginUrl="custom-login.aspx" protection="All" timeout="30" /forms /authentication authorization deny users="?" / /authorization /system.web custom-login.aspx文件,基本原理仍是跟2中說的同樣,如: if (blnIsAuthenticated) //注意這個blnIsAuthenticated是一個本身定義的變量 //當咱們把用戶輸入的信息和數據庫(或xml)的信息比對,存在則把該變量設爲true,反之false //這是跟2不同的地方 { FormsAuthentication.RedirectFromLoginPage(txtUsr.Value, chkPersist.Checked); //txtUsr和chkPersist分別爲textbox,checkbox控件 } else { //驗證失敗提示信息 } 剩下的如在其餘頁面得到用戶信息,如2同樣 4,退出登錄 響應退出登錄按鈕的代碼: FormsAuthentication.SignOut(); Response.Clear(); Response.Redirect(Request.UrlReferrer.ToString());//重定向到前一個頁面