Asp.Net的基於Forms的驗證機制–角色授權

來源:互聯網
上載者:User
構建基於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)

    如果需要儲存角色方式:  

  1.  FormsAuthenticationTicket authTicket = new    
  2.  FormsAuthenticationTicket(    
  3.  1, // version    
  4.  txtUserName.Text, // user name    
  5.  DateTime.Now, // creation    
  6.  DateTime.Now.AddMinutes(20),// Expiration    
  7.  false, // Persistent    
  8.  roles ); // User data   
  9.     //roles是一個角色字串數組    
  10.  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

  1. HttpCookie authCookie =    
  2. new HttpCookie(FormsAuthentication.FormsCookieName,    
  3. encryptedTicket);    
  4.   
  5. Response.Cookies.Add(authCookie);   
 HttpCookie authCookie =new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);Response.Cookies.Add(authCookie); 

   在Application_AuthenticateRequest事件中處理常式中(Global.asax)中,使用票建立IPrincipal對象並存在HttpContext.User中代碼:

  1. protected void Application_AuthorizeRequest(object sender, System.EventArgs e)   
  2. {   
  3.  HttpApplication App = (HttpApplication) sender;   
  4.  HttpContext Ctx = App.Context ; //擷取本次Http請求相關的HttpContext對象   
  5.  if (Ctx.Request.IsAuthenticated == true) //驗證過的使用者才進行role的處理   
  6.  {   
  7.  FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ;   
  8.  FormsAuthenticationTicket Ticket = Id.Ticket ; //取得身分識別驗證票   
  9.  string[] Roles = Ticket.UserData.Split (',') ; //將身分識別驗證票中的role資料轉成字串數組   
  10.  Ctx.User = new GenericPrincipal (Id, Roles) ; //將原有的Identity加上角色資訊建立一個GenericPrincipal表示目前使用者,這樣目前使用者就擁有了role資訊   
  11.  }   
  12. }  
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中加

  1. <location path="EditPost.aspx">    
  2. <system.web>    
  3. <authorization>    
  4. <allow roles="RoleName" />    
  5. <deny users="?" />    
  6. </authorization>    
  7. </system.web>    
  8. </location>   
 <location path="EditPost.aspx"><system.web><authorization><allow roles="RoleName" /><deny users="?" /></authorization></system.web></location> 

    2、把只能是某種角色訪問的檔案放在同一目錄下,在此目錄下添加一個web.config

  1. <configuration>    
  2. <system.web>    
  3. <authorization>    
  4. <allow roles="RoleName" />    
  5. <deny users="*" />    
  6. </authorization>    
  7. </system.web>    
  8. </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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.