自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談ASP.NET Forms驗證

開發(fā) 后端
本文介紹ASP.NET Forms驗證,當(dāng)然用戶也可以自定義與驗證方法,而最常用的莫過于Forms驗證,這也是今天所要討論的驗證方式。

ASP.NET Forms驗證

用戶驗證是每一個項目必須的一個模塊,由于已經(jīng)很久沒有碰到這一塊內(nèi)容,今天寫一個用戶驗證居然腦子一片空白。于是乎就和一個同事進(jìn)行了一片討論,晚上回家決定把討論的結(jié)果給記錄下來,以備后來之需。在ASP.NET中有幾種用戶驗證的方法:Windows驗證,F(xiàn)orms驗證和Passport驗證。當(dāng)然用戶也可以自定義與驗證方法,而最常用的莫過于Forms驗證,這也是今天所要討論的驗證方式。

ASP.NET Forms驗證方式首先要配置的是web.config文件,把authentication節(jié)點配置為Forms驗證,而它默認(rèn)的是Windows驗證。再修改配置文件時,還要注意大小寫,因為XML文件是大小寫敏感的,修改后authentication節(jié)點如下所示,其中還包含了一些form的配置參數(shù)。

  1. <authenticationmodeauthenticationmode="Forms"> 
  2. <forms 
  3. protection="All" 
  4. timeout="20" 
  5. name=".XDOTNET" 
  6. loginUrl="SignIn.aspx" 
  7. defaultUrl="Default.aspx" 
  8. path="/" 
  9. requireSSL="false" 
  10. enableCrossAppRedirects="false" 
  11. > 
  12. </forms> 
  13. </authentication> 

關(guān)于forms節(jié)點的屬性在后面介紹FormsAuthetication類的有關(guān)成員時,再介紹它們的用處。用戶驗證,顧名思義就是驗證用戶的合理性,當(dāng)用戶登錄到網(wǎng)站時,驗證輸入的用戶名和密碼是否和數(shù)據(jù)庫中存儲的數(shù)據(jù)相符合。其實很簡單,有一種快速的方法,這種驗證方法很適合后臺管理的驗證,因為當(dāng)我們關(guān)閉瀏覽器時驗證就會失效。

  1. publicstaticboolValidUser(stringuserName,stringpassword)  
  2. {  
  3. if(!string.IsNullOrEmpty(userName)&&!string.IsNullOrEmpty(password))  
  4. {  
  5. password=FormsAuthentication.HashPasswordForStoringInConfigFile(password,"MD5");  
  6. stringrealPassword=Users.GetUser(userName).Password;  
  7. if(string.Compare(password,realPassword,true)==0)  
  8. {  
  9. FormsAuthentication.SetAuthCookie(userName,false);  
  10. returntrue;  
  11. }  
  12. }  
  13. returnfalse;  

上面的方法就可以驗證以32位MD5加密的Password的數(shù)據(jù)驗證,其中Users.GetUser(string)這個方法是通過用戶名從數(shù)據(jù)庫中取得用戶實例。當(dāng)用戶合理時,通過FormsAuthentication.SetAuthCookie方法將為用戶(以用戶名)創(chuàng)建一個身份驗證票證,并將其添加到響應(yīng)的 Cookie 集合或 URL(cookieless)。這樣就實現(xiàn)了用戶驗證的過程,那么我們怎么得到用戶是否通過驗證呢?微軟把程序不斷的進(jìn)行封裝,不斷的傻瓜化,當(dāng)然想得到當(dāng)前用戶是否通過驗證也很簡單,代碼如下:

  1. public static bool IsAuthenticated()   
  2. {  
  3. return HttpContext.Current.User.Identity.IsAuthenticated;  


是不是很簡單呢?當(dāng)用戶(只要后臺管理驗證的情況下)驗證只要這兩個步驟就OK了,當(dāng)用戶登錄如調(diào)用ValidUser方法,當(dāng)載入頁面時通過 IsAuthenticated方法判斷當(dāng)前用戶是否通過驗證。這樣一個用戶驗證模塊也就完成了,但是在現(xiàn)代的網(wǎng)絡(luò)中,用戶是相當(dāng)?shù)闹靛X的東東,每個網(wǎng)站都會想留住很多的用戶;有時有些東西只允許會員才能夠查看等等,這樣就需要更好的驗證。使用戶關(guān)閉瀏覽器后,在一段特定時間內(nèi)還處于通過驗證狀態(tài)。這就需要操作和設(shè)置驗證的票據(jù)FormsAuthenticationTicket,代碼如下:

  1. public static bool ValidUser(string userName, string password)   
  2. {  
  3. if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))   
  4. {  
  5. password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");  
  6. string realPassword = Users.GetUser(userName).Password;  
  7. if (string.Compare(password, realPassword, true) == 0)   
  8. {  
  9. FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,  
  10. userName,  
  11. DateTime.Now,  
  12. DateTime.Now.AddMinutes(20),  
  13. false,  
  14. null//可以將Roles按","分割成字符串,寫入cookie  
  15. );  
  16. string data = FormsAuthentication.Encrypt(ticket);  
  17. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, data);  
  18. cookie.Path = FormsAuthentication.FormsCookiePath;  
  19. cookie.Domain = FormsAuthentication.CookieDomain;  
  20. cookie.Expires = ticket.Expiration;  
  21. HttpContext.Current.Response.Cookies.Add(cookie);  
  22. return true;  
  23. }  
  24. }  
  25. return false;  

從代碼中看到的FormsCookiePath,CookieDomain等等就是從配置文件中獲得,關(guān)于其它的FormsAuthentication成員可以訪問MSDN(FormsAuthentication)。我們同樣也可以通過HttpContext.Current.User對象來判斷當(dāng)前用戶的狀況,也可以用IsInRole方法來判斷用戶的角色。當(dāng)然當(dāng)我們驗證用戶后,要把用戶加入到Http上下文HttpContext的當(dāng)前請求的User對象中,代碼如下:

  1. FormsIdentity identity = new FormsIdentity(ticket);  
  2. GenericPrincipal user = new GenericPrincipal(identity, new string[] { });  
  3. HttpContext.Current.User = user

這樣就完成了ASP.NET Forms驗證的全過程。至于查看用戶的Cookie判斷用戶是否存在記錄狀態(tài)(如:記錄1個月,1天,1年等等),可以在管道中進(jìn)行判斷和編寫,這里就不再贅述。OK,由于時間的關(guān)系,就記錄這些,如果有什么錯誤或更好的方法請大家指出,謝謝。

【編輯推薦】

  1. ASP.NET開發(fā)技巧之Theme功能淺析
  2. 詳解ASP.NET動態(tài)編譯
  3. Apache支持ASP.NET方法淺析
  4. 淺談ASP.NET服務(wù)器標(biāo)準(zhǔn)控件
  5. ASP.NET中SQL Server數(shù)據(jù)庫備份恢復(fù)淺析
責(zé)任編輯:佚名 來源: cnblogs
相關(guān)推薦

2009-07-29 09:59:10

ASP.NET For

2009-08-05 16:50:09

ASP.NET For

2009-08-05 16:17:29

ASP.NET For

2011-05-23 10:37:03

2012-04-16 09:54:26

2009-07-24 10:52:42

ASP.NET ISA

2009-07-22 16:11:43

ASP.NET AJA

2009-07-20 15:30:11

ASP.NET應(yīng)用

2009-09-23 09:19:23

2009-07-23 15:44:39

ASP.NET MVC

2009-07-28 17:49:30

ASP.NET定期回收

2009-07-20 17:12:17

ASP.NET訪問數(shù)據(jù)

2009-07-28 16:03:23

ASP.NET狀態(tài)服務(wù)

2009-08-10 10:19:47

ASP.NET組件設(shè)計

2009-07-22 13:24:24

ASP.NET MVC

2009-07-23 15:24:37

ASP.NET Ima

2011-08-23 10:58:59

2009-07-21 14:16:02

ASP.NET管道優(yōu)化

2009-07-27 16:19:59

ASP.NET報表控件

2009-08-05 13:09:17

ASP.NET應(yīng)用執(zhí)行
點贊
收藏

51CTO技術(shù)棧公眾號