ASP.NET 2.0 Internet安全之參考實現

[來源: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());//重定向到前一個頁面

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章