2015-05-02 58 views
0
I have implemented session sliding using in my customehttphandler module. 

我想嘗試會話滑動以及獲得多個網站上共享相同的ADFS服務器的身份驗證。ADFS新鮮度和會話滑動

public void SessionAuthenticationModuleSessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e) 
     { 
      SessionSecurityToken token = e.SessionToken; 
      DateTime nowUtc = DateTime.UtcNow; 
      DateTime validFrom = token.ValidFrom; 
      DateTime validTo = token.ValidTo; 
      double totalMinutes = (validTo - validFrom).TotalMinutes; 
      double halfSpan = totalMinutes/2; 

      SessionAuthenticationModule sam = sender as SessionAuthenticationModule; 

      if (validTo < nowUtc) 
      { 
       if (sam != null) 
       { 
        sam.DeleteSessionTokenCookie(); 
        e.Cancel = true; 
       }    
      } 
      else if ((nowUtc - validFrom).TotalMinutes >= halfSpan) 
      { 
       SessionSecurityToken renewToken =    sam.CreateSessionSecurityToken(
        token.ClaimsPrincipal, 
        token.Context, 
        nowUtc, 
        nowUtc.AddMinutes(totalMinutes), 
        true); 
       e.SessionToken = renewToken; 

       e.ReissueCookie = true; 

//db timestamp update 
      } 
     } 

And SignedIn event 

public void WSFederationAuthenticationModuleSignedIn(object sender, EventArgs e) 
     { 

      token = gettoken from cookie 
      if (token.ValidTo > DateTime.Now.ToUniversalTime()) 
      { 
        //db insert for new login (assuming this will fire only  once on actual login) 
        reissue token 
      } 
     } 

Session timeout is mentioned in the my relying party application web config 

<securityTokenHandlers> 
     <add type="Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> 
      <sessionTokenRequirement lifetime="0:02" /> 
     </add> 
     </securityTokenHandlers> 

Token Life time on ADFS I do not want to change which is greater than 2 minutes. 

But issue is, after 2 minutes time out is not happening. It goes to SingedIn event becuase i assume it reissue token and then it calls session token received event so this condition (if (validTo < nowUtc)) never satisfy, how can i achieve timeout here? Freshness="0"achieves it but If i set Freshness="0" then I can not get authenticated by other website which are on same ADFS server. I want to be authenticated on other website as well if i have logged in one. 

If I remove freshness="0" I can be authenticated without login on second website which is different application. 

Why SignedIn is getting called before session token received and How can i achieve timeout in proper way and get authenticated in multiple website? 

注意:我在我的customeHttpHanlder模塊中有這些事件。其中還有其他事件,例如PostAuthenticateRequest。

回答

1

當您收到會話令牌時,您從adfs收到的令牌開始過期。它完全失效後需要刷新。

  • 這是具有ADFS(調入廣告你想知道一些關於用戶每次)acurate的信息,並具有可操作性的情況之間的平衡(一個簽名令牌在我們信任這些信息一定的有效期限保持有效)。令牌到期

後,你需要獲得回ADFS(因此登入活動),以獲得從ADFS一個新的令牌。這個想法是,一些信息可能在發佈這兩個令牌之間發生了變化。

您可以在客戶端(您的依賴方)實施滑動會話,但這沒什麼意義(我會回過頭來看),因爲您告訴自己該令牌在另一段時間內有效。你相信自己,但令牌內的信息可能會不同步,這就是爲什麼你總是需要回到adfs。

所有這可能是有意義的,如果你自己實現自動刷新令牌。這意味着您將您的當前令牌換成具有新有效期的新令牌。我想adfs可以做到這一點(但你需要這種活躍的場景)。這不是很多代碼,但它可能是地獄設置正確,我沒有任何這方面的例子。

最後,你需要問問自己是否值得冒險。 WIF將再次執行自動登錄,域內的用戶將自動登錄。域外的用戶可能需要再次輸入憑證。我不認爲這是世界末日。

  • 最後,我看到你使用Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler這是舊的實現。 .Net 4.5有更新的實現..