構建基於forms的驗證機制過程如下:
1,設定IIS為可匿名訪問和asp.net web.config中設定為form驗證
2,檢索資料存放區驗證使用者,並檢索角色(如果不是基於角色可不用)
簡單無role方式:
使用FormsAuthenticationTicket建立一個Cookie並回傳到用戶端,並儲存 角色到票中,如:
FormsAuthentication.SetAuthCookie(Username,true | false)
cookies儲存時間:
HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.Now.AddDays(1)
如果需要儲存角色方式:
- FormsAuthenticationTicket authTicket = new
- FormsAuthenticationTicket(
- 1, // version
- txtUserName.Text, // user name
- DateTime.Now, // creation
- DateTime.Now.AddMinutes(20),// Expiration
- false, // Persistent
- roles ); // User data
- //roles是一個角色字串數組
- string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //加密
FormsAuthenticationTicket authTicket = newFormsAuthenticationTicket(1, // versiontxtUserName.Text, // user nameDateTime.Now, // creationDateTime.Now.AddMinutes(20),// Expirationfalse, // Persistentroles ); // User data //roles是一個角色字串數組string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //加密
存入Cookie
- HttpCookie authCookie =
- new HttpCookie(FormsAuthentication.FormsCookieName,
- encryptedTicket);
-
- Response.Cookies.Add(authCookie);
HttpCookie authCookie =new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);Response.Cookies.Add(authCookie);
在Application_AuthenticateRequest事件中處理常式中(Global.asax)中,使用票建立IPrincipal對象並存在HttpContext.User中代碼:
- 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資料轉成字串數組
- Ctx.User = new GenericPrincipal (Id, Roles) ; //將原有的Identity加上角色資訊建立一個GenericPrincipal表示目前使用者,這樣目前使用者就擁有了role資訊
- }
- }
protected void PostAuthenticateRequest(Object sender, 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資料轉成字串數組Ctx.User = new GenericPrincipal (Id, Roles) ; //將原有的Identity加上角色資訊建立一個GenericPrincipal表示目前使用者,這樣目前使用者就擁有了role資訊}}
需要對某些頁面進行角色控制,有兩種方法:
1、web.config中加
- <location path="EditPost.aspx">
- <system.web>
- <authorization>
- <allow roles="RoleName" />
- <deny users="?" />
- </authorization>
- </system.web>
- </location>
<location path="EditPost.aspx"><system.web><authorization><allow roles="RoleName" /><deny users="?" /></authorization></system.web></location>
2、把只能是某種角色訪問的檔案放在同一目錄下,在此目錄下添加一個web.config
- <configuration>
- <system.web>
- <authorization>
- <allow roles="RoleName" />
- <deny users="*" />
- </authorization>
- </system.web>
- </configuration>
<configuration><system.web><authorization><allow roles="RoleName" /><deny users="*" /></authorization></system.web></configuration>
說明:子目錄的web.config設定優先於父目錄的web.config設定
Forms身分識別驗證,為什麼<allow roles="Administrators" /><deny users="*" />後,所有使用者都進不來了?
把把授權的代碼放到Application_AuthorizeRequest裡面是不對的!由於很多相關文章都是引用的,故害死很好程式員,應該是放在下面這個事件裡的。
void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpApplication App = (HttpApplication)sender;
HttpContext Ctx = App.Context; //擷取本次Http請求相關的HttpContext對象
if (Ctx.Request.IsAuthenticated) //驗證過的使用者才進行role的處理
{
FormsIdentity Id = Ctx.User.Identity as FormsIdentity;
FormsAuthenticationTicket Ticket = Id.Ticket; //取得身分識別驗證票
string[] Roles = Ticket.UserData.Split(','); //將身分識別驗證票中的role資料轉成字串數組
Ctx.User = new System.Security.Principal.GenericPrincipal(Id, Roles); //將原有的Identity加上角色資訊建立一個GenericPrincipal表示目前使用者,這樣目前使用者就擁有了role資訊
}
}
具體請參考:http://community.csdn.net/Expert/TopicView3.asp?id=5526963