2013-10-23 35 views
0

在ASP.net WebForms項目中,我們當前使用了窗體身份驗證,但客戶端希望使用Windows身份驗證。所以我更改了web.config以使用Windows身份驗證。但是,應用程序需要一些用戶輸入並在可以訪問任何網頁之前投入會話。我們目前在登錄頁面的回發中執行此操作。Windows身份驗證初始化ASP.net

由於Windows身份驗證沒有「登錄」頁面,因此如何實現?如果會話設置正確,我是否應該檢查每個頁面上的On_Init事件?

回答

2

如果您需要每個頁面中可用會話中的特定數據,最簡單的方法是使用專用的模塊,該模塊檢查會話可用的早期管道事件之一(獲取請求狀態事件聽起來最合適)。

public class CustomConditionCheckModule : IHttpModule 
{ 
    public void Init(HttpApplication context) 
    { 
     context.AcquireRequestState += new EventHandler(acq_req_state); 
    }   

    public void acq_req_state(object sender, EventArgs e) 
    { 
     // check your condition there - the data is in session 
     var session = HttpContext.Current.Session; 
     if (... the condition ...) 
      Response.Redirect("~/anonymous.page.aspx"); 
    } 
} 

然後,你還需要一個可匿名訪問的(你需要在web.config此一節)的頁面:

<location path="anonymous.page.aspx"> 
    <system.web> 
    <authorization> 
     <allow users="*" /> 
    </authorization> 
    </system.web> 
</location> 
-1

我想你可以去LDAP(Active Directory驗證) 使用Windows身份驗證在web.config中,但如果它的內網Web應用程序或另一種方式,即基於角色安全與Windows身份驗證。

+0

這不回答這個問題。 –

+0

@WiktorZychla:實際上,我回答考慮使用LDAP的Windows身份驗證可以具有自定義登錄頁面,其餘事情可以通常的方式實現(如他所做的那樣)。因爲,我也正如我剛纔提到的那樣爲我目前的項目做同樣的事情。但是,如果它不是那樣意味着客戶不想要那麼那麼你就是正確的方式。 –

+0

你可以嘗試擴大你的答案。 –