2012-11-08 37 views
3

我正在使用WIF和WS Federation,以便我的ASP.NET應用程序可以通過STS(Thinktecture IdentityServer)進行身份驗證。在我的RP中,我想實際設置基於用戶聲明的cookie持久性。如何在SessionSecurityTokenCreated時有條件地設置FedAuth cookie SessionToken.IsPersistent

觀看Fiddler中的流量我可以看到WIF FedAuth Cookie是在STS令牌發佈到RP時首先設置的。在設置cookie之前,我想攔截一些事件,並根據當前的聲明將cookie設置爲持久(或不)。

我知道我可以在web.config中設置cookie持久性,但是這種行爲需要基於用戶的條件。

<wsFederation ... persistentCookiesOnPassiveRedirects="true" /> 

我的第一種方法是嘗試處理各種SessionSecurityTokenCreated事件,但這些事件似乎從來沒有被解僱。 我是否錯誤地添加了處理程序?或者有更好的方法來做到這一點?

protected void Application_Start() 
{ 
    ... 

    FederatedAuthentication.SessionAuthenticationModule.SessionSecurityTokenCreated += 
     new EventHandler<SessionSecurityTokenCreatedEventArgs>(SessionAuthenticationModule_SessionSecurityTokenCreated); 

    FederatedAuthentication.WSFederationAuthenticationModule.SessionSecurityTokenCreated += 
     new EventHandler<SessionSecurityTokenCreatedEventArgs>(WSFederationAuthenticationModule_SessionSecurityTokenCreated); 

} 



//This never seems to fire... 
void SessionAuthenticationModule_SessionSecurityTokenCreated(object sender, 
     SessionSecurityTokenCreatedEventArgs e) 
{ 
    if (e.SessionToken.ClaimsPrincipal.HasClaim("someClaim", "someValue")) 
     e.SessionToken.IsPersistent = true; 
    else 
     e.SessionToken.IsPersistent = false; 
} 


//This never seems to fire either... 
void WSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender, 
     SessionSecurityTokenCreatedEventArgs e) 
{ 
    if (e.SessionToken.ClaimsPrincipal.HasClaim("someClaim", "someValue")) 
     e.SessionToken.IsPersistent = true; 
    else 
     e.SessionToken.IsPersistent = false;    

} 

有趣的注意的是:如果我添加一個處理SessionAuthenticationModule_SessionSecurityTokenReceived此事件似乎觸發。在這裏,我可以重新發出cookie並設置IsPersistent = true但是直到第一次設置cookie之後纔會被解僱,而且當cookie首次發佈時我更願意這樣做。

測試了一下之後:如果我重新發出SessionAuthenticationModule_SessionSecurityTokenReceived中的cookie,那麼會觸發SessionAuthenticationModule_SessionSecurityTokenCreated。當令牌首次發佈到RP時,我似乎無法找出爲什麼不在cookie的初始創建時被解僱。

+0

看來,WSFederationAuthenticationModule事件沒有被解僱了,因爲我被分配一個自定義類在web.config從WSFederationAuthenticationModule獲得。我不知道爲什麼自定義WSFederationAuthenticationModule沒有觸發事件(或者執行我在App_startup中添加的處理程序。 – gmetzker

回答

2

我的問題的來源是: a)我正在使用自定義的WSFederationAuthenticationModule。 b)我沒有使用自定義模塊的名稱來連接Global.asax中的事件。

假設我的web.config中有這樣的:

<system.webServer> 

// ... 

    <add name="MyCustomWSFederationAuthenticationModule" 
    type="MyLib.MyCustomWSFederationAuthenticationModule, Thinktecture.IdentityModel, Version=1.0.0.0, Culture=neutral" 
    preCondition="managedHandler" /> 

    <add name="SessionAuthenticationModule" 
    type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
    preCondition="managedHandler" /> 


// ... 

</system.webServer> 

並假設「MyCustomWSFederationAuthenticationModule」是自定義補身份驗證模塊的名稱。然後,我只需修復方法處理程序的名稱(沒有任何應用程序啓動)。

protected void Application_Start() 
{ 
    //Nothing here. 
} 

//This never seems to fire either... 
void MyCustomWSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender, 
     SessionSecurityTokenCreatedEventArgs e) 
{ 
    if (e.SessionToken.ClaimsPrincipal.HasClaim("someClaim", "someValue")) 
     e.SessionToken.IsPersistent = true; 
    else 
     e.SessionToken.IsPersistent = false;    
} 
相關問題