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

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

 

咱們在登錄成功後,使用下面的3種方法,都是同一個目的:建立身份驗證票並將其附加到 Cookie,html

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

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.SetAuthCookiecookie

演示:asp.net

 

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,並認爲登陸有效。請看代碼:this

image

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

2:FormsAuthenticationTicketspa

演示:.net

複製代碼
////建立身份驗證票
   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 方法獲取到用戶原先請求的頁面,重定向到這個頁面。 
一、在瀏覽器上建立一個cookie,其中包含一個驗證令牌。 
二、返回剛纔您所請求的頁面; 
至關於這兩句: 
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步走: 
一、建立身份驗證票 
二、加密身份驗證票 
三、生成Cookie 
四、Cookie輸出到客戶端 
五、頁面重定向

 

其餘參考:

(1):用戶名輸入框可以記住用戶名,免得下次從新輸入

(2):細說Cookie

相關文章
相關標籤/搜索