ASP.NET HTTP模組和處理常式(6)

來源:互聯網
上載者:User
實現一個提供安全服務的HTTP模組
  
    現在我們實現一個HTTP模組,它為我們的Web應用程式提供安全服務。該HTTP模組基本上是提供一種定製的身份認證服務。它將接收HTTP請求中的身份憑證,並確定該憑證是否有效。如果有效,與使用者相關的角色是什嗎?通過User.Identity對象,它把這些角色與訪問我們的Web應用程式頁面的使用者的標識關聯起來。
  下面是該HTTP模組的代碼:
  
  using System;
  using System.Web;
  using System.Security.Principal;
  
  namespace SecurityModules
  {
   /// Class1的總體描述。
  
   public class CustomAuthenticationModule : IHttpModule
   {
    public CustomAuthenticationModule()
    {
    }
    public void Init(HttpApplication r_objApplication)
    {
     // 向Application 對象註冊事件處理常式。
     r_objApplication.AuthenticateRequest +=
  new EventHandler(this.AuthenticateRequest) ;
    }
  
    public void Dispose()
    {
     // 此處空出,因為我們不需要做什麼操作。
    }
  
    private void AuthenticateRequest(object r_objSender,EventArgs r_objEventArgs)
    {
     // 鑒別使用者的憑證,並找出使用者角色。。
     1. HttpApplication objApp = (HttpApplication) r_objSender ;
     2. HttpContext objContext = (HttpContext) objApp.Context ;
     3. if ( (objApp.Request["userid"] == null) ||
     4.  (objApp.Request["password"] == null) )
     5.  {
     6.   objContext.Response.Write("<H1>Credentials not provided</H1>") ;
     7.   objContext.Response.End() ;
     8.  }
  
     9. string userid = "" ;
     10. userid = objApp.Request["userid"].ToString() ;
     11. string password = "" ;
     12. password = objApp.Request["password"].ToString() ;
   
     13. string[] strRoles ;
     14. strRoles = AuthenticateAndGetRoles(userid, password) ;
     15. if ((strRoles == null) || (strRoles.GetLength(0) == 0))
     16. {
     17.  objContext.Response.Write("<H1>We are sorry but we could not
  find this user id and password in our database</H1>") ;
     18.  objApp.CompleteRequest() ;
     19. }
  
     20. GenericIdentity objIdentity = new GenericIdentity(userid,
  "CustomAuthentication") ;
     21. objContext.User = new GenericPrincipal(objIdentity, strRoles) ;
    }
  
    private string[] AuthenticateAndGetRoles(string r_strUserID,string r_strPassword)
    {
     string[] strRoles = null ;
     if ((r_strUserID.Equals("Steve")) && (r_strPassword.Equals("15seconds")))
     {
      strRoles = new String[1] ;
      strRoles[0] = "Administrator" ;
     }
     else if ((r_strUserID.Equals("Mansoor")) && (r_strPassword.Equals("mas")))
     {
      strRoles = new string[1] ;
      strRoles[0] = "User" ;
     }
     return strRoles ;
    }
   }
  }
  
    我們研究一下上面的代碼。
  
    我們是從Init函數開始的。這個函數把處理常式的AuthenticateRequest事件插入Application(應用程式)對象的事件處理常式列表中。這將導致引發AuthenticationRequest事件的時候Application調用該方法。
  
    我們的HTTP模組初始化之後,我們就可以調用它的AuthenticateRequest方法來鑒別用戶端請求。AuthenticateRequest方法是該安全/身份認證機制的核心。在這個函數中:
  
    1和2行提取HttpApplication和HttpContext對象。3到7行檢測是否沒有給我們提供了使用者id或密碼。如果沒有提供,就顯示錯誤資訊,請求處理過程終止。
  
    9到12行從HttpRequest對象中提取使用者id和密碼。
  
    14行調用一個叫做AuthenticateAndGetRoles的輔助(helper)函數。這個函數主要執行身分識別驗證並決定使用者角色。上面的代碼採用了寫入程式碼(hard-coded),只允許兩個使用者使用,但是我們可以擴充這個方法,並添加代碼與使用者資料庫互動操作並檢索使用者的角色。
  
    16到19行檢測是否有角色與使用者關聯。如果沒有就意味著傳遞給我們的憑證沒有通過驗證;因此該憑證是無效的。因此,給用戶端發送一個錯誤資訊,並且請求結束了。
  
    20和21行非常重要,因為這兩行實際上告訴ASP.NET HTTP運行時已登入使用者的身份。這兩行成功執行以後,我們的aspx頁面就能夠使用User對象訪問這些資訊了。
  
    現在我們看一看這種身分識別驗證機制的運行情況。目前我們只允許下面兩個使用者登入到系統:
  
    · User id = Steve, Password = 15seconds, Role = Administrator
    · User id = Mansoor, Password = mas, Role = User
  
    注意使用者id和密碼是大小寫敏感的(區分大小寫)。
  
    首先試圖不提供憑證登入系統,在IE中輸入http://localhost/webapp2/index.aspx將看到下面的訊息:
  
   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.