欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.net Forms驗證Demo

 更新時間:2009年01月03日 00:47:41   作者:  
Asp.net中基于Forms驗證的角色驗證授權

Forms身份驗證基于角色的授權

一 身份驗證

在web.config的<authentication>的設置還是一樣:

<authentication mode="forms">
<forms name=".ASPXAUTH" loginUrl="/login.aspx" timeout="30" path= "/">
</forms>
</authentication>

/login.aspx驗證用戶合法性頁面中,在驗證了用戶的合法性后,還要有個取得此用戶屬于哪些role的過程,這個看各個應用的本身如何設計的了,一般是在數(shù)據(jù)庫中會有個use_role表,可以從數(shù)據(jù)庫中獲得此用戶屬于哪些role,在此不深究如何去獲取用戶對應的role,最后肯定能夠獲得的此用戶對應的所有的role用逗號分割的一個字符串。
在上面的非基于角色的方法中,我們用了FormsAuthentication.RedirectFromLoginPage 方法來完成生成身份驗證票,寫回客戶端,瀏覽器重定向等一系列的動作。這個方法會用一些確省的設置來完成一系列的動作,在基于角色的驗證中我們不能用這一個方法來實現(xiàn),要分步的做,以便將一些定制的設置加進來:

1. 首先要根據(jù)用戶標示,和用戶屬于的角色的字符串來創(chuàng)建身份驗證票
public FormsAuthenticationTicket(
int version, //設為1
string name, //用戶標示
DateTime issueDate, //Cookie 的發(fā)出時間, 設置為 DateTime.Now
DateTime expiration, //過期時間
bool isPersistent, //是否持久性(根據(jù)需要設置,若是設置為持久性,在發(fā)出
cookie時,cookie的Expires設置一定要設置)
string userData, //這里用上面準備好的用逗號分割的role字符串
string cookiePath // 設為"/",這要同發(fā)出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集合中,發(fā)送到客戶端.
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); //調(diào)用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)
{
//相應的代碼
}

二 基于角色訪問授權

這里我們要做的是,將客戶端保存的身份驗證票中UserData中保存的表示角色的信息恢復到在服務端表示用戶身份的GenericPrincipal對象中(記住,原來的驗證過程中, GenericPrincipal對象只包含了用戶信息,沒有包含role信息)
一個Http請求的過程中,HttpApplication.AuthenticateRequest事件表示安全模塊已建立用戶標識,就是此用戶的身份在web端已經(jīng)建立起來, 在這個事件之后我們就可以獲取用戶身份信息了.
在HttpApplication.ResolveRequestCache事件之前,asp.net開始取得用戶請求的頁面,建立HttpHandler控制點,這時就已經(jīng)要驗證用戶的權限了,所以恢復用戶角色的工作只能在HttpApplication.AuthenticateRequest事件和HttpApplication.ResolveRequestCache事件之間的過程中做.
我們選擇Application_AuthorizeRequest事件中做這個工作,可以在global.asax文件中處理HttpApplication的所有的事件,代碼如下:

protected void Application_AuthorizeRequest(object sender, System.EventArgs e)
{
HttpApplication App = (HttpApplication) sender;
HttpContext Ctx = App.Context ; //獲取本次Http請求相關的HttpContext對象
if (Ctx.Request.IsAuthenticated == true) //驗證過的用戶才進行role的處理
{
FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ;
FormsAuthenticationTicket Ticket = Id.Ticket ; //取得身份驗證票
string[] Roles = Ticket.UserData.Split (',') ; //將身份驗證票中的role數(shù)據(jù)轉成字符串數(shù)組
Ctx.User = new GenericPrincipal (Id, Roles) ; //將原有的Identity加上角色信息新建一個GenericPrincipal表示當前用戶,這樣當前用戶就擁有了role信息
}
}

訪問者同時具有了user和role信息,就可以據(jù)此在web.config中用role來控制用戶的訪問權限了.

相關文章

最新評論