2012-10-02 56 views
1

我正在開發.net 4.0中的應用程序。在我的Web應用程序項目中,它已保護文件夾。 現在我想只將經過身份驗證的用戶重定向到受保護的頁面。IIS上的兩個虛擬目錄用於安全頁面

我想(多了一個目錄中的虛擬目錄)做這在IIS做兩個不同的虛擬目錄。

我可以做到這一點?我怎樣才能做到這一點?或者有什麼辦法呢?

謝謝先進。

回答

0

如果您喜歡使用您的代碼手動執行此操作,則正確的位置在global.asaxApplication_AuthenticateRequest觸發器上。這裏是一個例子,你可以根據你的邏輯改變它。

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
    string cTheFile = HttpContext.Current.Request.Path; 

    if(!cTheFile.Contains("/securedir/")) 
    { 
    if (HttpContext.Current.Request.IsSecureConnection) 
    { 
     HttpApplication app = (HttpApplication)sender; 
     string cookieName = FormsAuthentication.FormsCookieName; 
     HttpCookie authCookie = Context.Request.Cookies[cookieName]; 

     // if this is null, then the user is not logged in 
     if (null == authCookie) 
     { 
    // for the same check you can also use 
    //if(HttpContext.Current.User == null || HttpContext.Current.User.Identity == null || !HttpContext.Current.User.Identity.IsAuthenticated) 

      string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile); 

      if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase)) 
      { 
       Response.Redirect("/", true); 
       Response.End(); 

       // There is no authentication cookie. 
       return; 
      } 
     } 
     } 
    } 
    } 
}