asp.net 登陸驗證 Form表單驗證的3種方式


asp.net 登陸驗證 Form表單驗證的3種方式 FormsAuthentication.SetAuthCookie;FormsAuthentication.RedirectFromLoginPage;FormsAuthenticationTicket

 

我們在登陸成功后,使用下面的3種方法,都是同一個目的:創建身份驗證票並將其附加到 Cookie,

當我們用Forms認證方式的時候,可以使用HttpContext.Current.User.Identity.IsAuthenticated  (或者也可以用 Request.IsAuthenticated ,這個實際上也是調用的是User.Identity.IsAuthenticated來驗證)來判斷是否登陸;而這個判斷就是依賴於這個Cookie里的信息判斷用戶是否登陸。 
FormsAuthentication.SignOut用來清除這個Cookie標記

Form身份認證依賴Cookie,Asp.net就是每次檢查我們在配置文件中指定的Cookie名稱,並解密這個Cookie來判斷當前請求用戶的登錄狀態

使用下面3種方法的前提是在web.config里面設置為表單驗證

 

<authentication mode="Forms">
            <forms name=".MyCookie" loginUrl="Login.aspx" protection="All" timeout="60"/>
        </authentication> 

 

1:FormsAuthentication.SetAuthCookie

演示:

 

FormsAuthentication.SetAuthCookie(UserInfo.UserName, false, FormsAuthentication.FormsCookiePath);


image 

 

【System.Web.Security.FormsAuthentication.SetAuthCookie("fish", false);】后,Asp.net做了些什么, 回答這個問題其實很簡單:自己用Reflector.exe去看一下Asp.net的實現吧。 
這里為了更讓您能信服登錄與Cookie有關,我將直接創建一個Cookie看一下 Asp.net能不能認可我創建的Cookie,並認為登錄有效。請看代碼:

image

如果執行這段代碼,您將發現:【Request.IsAuthenticated】返回true,登錄狀態會顯示"已登錄"。 
至此,我們可以得出一個結論: Form身份認證依賴Cookie,Asp.net就是每次檢查我們在配置文件中指定的Cookie名稱,並解密這個Cookie來判斷當前請求用戶的登錄狀態。

2:FormsAuthenticationTicket

演示:

復制代碼
 ////創建身份驗證票
   FormsAuthenticationTicket AuTicket =new FormsAuthenticationTicket(
 1, UserInfo.UserName, DateTime.Now, DateTime.Now.AddMinutes(30),
 false, Request.UserHostAddress);
 ////將票據加密
  string authTicket = FormsAuthentication.Encrypt(AuTicket);
 ////將加密后的票據保存為cookie 
 HttpCookie coo =new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);
 coo.Secure =false;
 coo.Expires = AuTicket.Expiration;
 coo.Path = FormsAuthentication.FormsCookiePath;
 ////加入新cookie 
 Response.Cookies.Add(coo);
復制代碼

 

 

3:FormsAuthentication.RedirectFromLoginPage

演示:

FormsAuthentication.RedirectFromLoginPage(UserInfo.UserName, false);

注釋: 

名稱 說明
FormsAuthentication.RedirectFromLoginPage (String, Boolean) 將經過身份驗證的用戶重定向回最初請求的 URL 或默認 URL。
FormsAuthentication.RedirectFromLoginPage (String, Boolean, String) 使用 Forms 身份驗證 Cookie 的指定 Cookie 路徑,將經過身份驗證的用戶重定向回最初請求的 URL 或默認 URL。

 

FormsAuthentication.RedirectFromLoginPage的第二個參數,true表示保留持久cookie,過期時間就是web.config里的時間,如果是false則關閉瀏覽器就過期。

這一行代碼實現你在填寫登錄名和密碼后,成功就轉到原先你想到的頁面。

這后面的參數“false”說明是否永久保留cookie。True則表示永久保留,下次訪問就不用輸入密碼了,否則斷開本次鏈接后,下次還需要輸入密碼。這次參數也可以由用戶選擇,因為考慮到安全性,可以在用戶名或密碼的傍邊放個checkbox,原來的語句可以為:

System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.txtname.Text,this.CheckBox.Checked);

 

 

 

RedirectFromLoginPage和FormsAuthenticationTicket的區別

如果你對.net身份驗證不是很清晰,請看本文。本文用簡單明了的語言,讓你對RedirectFromLoginPage和FormsAuthenticationTicket有一個完整的認識。 
1)FormsAuthentication.RedirectFromLoginPage(UserName.Text, mycheckbox.Checked)用於基於用戶的驗證 
此方法封裝了生成身份驗證票,寫回客戶端,瀏覽器重定向等一系列的動作 
RedirectFromLoginPage()方法首先生成生成身份驗證票,然后調用FormAuthenticaiton.Encrypt() 方法,該方法將身份驗證票加密為字符串,然后生成身份驗證Cookie,再將此Cookie加入到Response.Cookies中,等待發送到客戶端。最后RedirectFromLoginPage方法調用FormsAuthentication.GetRedirectUrl 方法獲取到用戶原先請求的頁面,重定向到這個頁面。 
1、在瀏覽器上創建一個cookie,其中包含一個驗證令牌。 
2、返回剛才您所請求的頁面; 
相當於這兩句: 
FormsAuthentication.SetAuthCookie(UserName.Text,mycheckbox.Checked); 
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text,mycheckbox.Checked); 
也就是說FormsAuthentication.RedirectFromLoginPage方法相當於一個封裝的方法,簡化了很多細節。

2)FormsAuthenticationTicket,用於基於角色的身份驗證 
上面的非基於角色的方法中,用了FormsAuthentication.RedirectFromLoginPage 方法來完成生成身份驗證票,寫回客戶端,瀏覽器重定向等一系列的動作。這個方法會用一些確省的設置來完成一系列的動作,在基於角色的驗證中我們不能用這一個方法來實現,要分步的做,以便將一些定制的設置加進來:

1. 首先要根據用戶標示,和用戶屬於的角色的字符串來創建身份驗證票 
public FormsAuthenticationTicket( 
int version, //設為1 
string name, //用戶標示 
DateTime issueDate, //Cookie 的發出時間, 設置為 DateTime.Now 
DateTime expiration, //過期時間 
bool isPersistent, //是否持久性(根據需要設置,若是設置為持久性,在發出 
cookie時,cookie的Expires設置一定要設置) 
string userData, //這里用上面准備好的用逗號分割的role字符串 
string cookiePath // 設為"/",這要同發出cookie的路徑一致,因為刷新cookie 
要用這個路徑 
);

FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,"kent",DateTime.Now, DateTime.Now.AddMinutes(30), false,UserRoles,"/") ;

2. 生成身份驗證票的Cookie 
2.1 將身份驗證票加密序列化成一個字符串 
string HashTicket = FormsAuthentication.Encrypt (Ticket) ; 
2.2 生成cookie 
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ; 
FormsAuthentication.FormsCookieName 是用來獲取web.config中設置的身份驗證cookie的名字,缺省為" .ASPXAUTH". 
若身份驗證票中的isPersistent屬性設置為持久類,則這個cookie的Expires屬性一定要設置,這樣這個cookie才會被做為持久cookie保存到客戶端的cookie文件中. 
3. 將身份驗證票Cookie輸出到客戶端 
通過Response.Cookies.Add(UserCookie) 將身份驗證票Cookie附加到輸出的cookie集合中,發送到客戶端. 
4. 重定向到用戶申請的初試頁面.

驗證部分代碼(這部分代碼是在login.aspx頁面上點擊了登錄按鈕事件處理代碼):

private void Buttonlogin_Click(object sender, System.EventArgs e) 

     string user = TextBoxUser.Text; //讀取用戶名 
     string password = TextBoxPassword.Text; //讀取密碼 
     if(Confirm(user,password) == true) //confirm方法用來驗證用戶合法性的 
    { 
         string userRoles = UserToRole(user); //調用UserToRole方法來獲取role字符串 
         FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,user,DateTime.Now,          DateTime.Now.AddMinutes(30), false,userRoles,"/") ; //建立身份驗證票對象 
         string HashTicket = FormsAuthentication.Encrypt (Ticket) ; //加密序列化驗證票為字符串 
         HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ; 
//生成Cookie 
          Context.Response.Cookies.Add (UserCookie) ; //輸出Cookie 
         Context.Response.Redirect (Context.Request["ReturnUrl"]) ; // 重定向到用戶申請的初始頁面 
     } 
    else 
    { 
        // 用戶身份未被確認時的代碼 
    } 

//此方法用來驗證用戶合法性的 
private bool Confirm(string user,string password) 

    //相應的代碼 

//此方法用來獲得的用戶對應的所有的role用逗號分割的一個字符串 
private string UserToRole(string user) 

    //相應的代碼 
}

3)總結 
身份驗證5步走: 
1、創建身份驗證票 
2、加密身份驗證票 
3、生成Cookie 
4、Cookie輸出到客戶端 
5、頁面重定向

 

其他參考:

(1):用戶名輸入框能夠記住用戶名,省得下次重新輸入

(2):細說Cookie

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM