2011-11-14 88 views
5

我試圖建立滑動WIF會議和需要處理SessionSecurityTokenReceived如何在Global.asax中處理事件SessionSecurityTokenReceived?

我敢肯定,我做了愚蠢的事情在這裏......但VS2010不斷告訴我,There is no applicable variable or member現貨如下圖所示。任何人都可以將我指向正確的方向嗎?我已經搜索瞭如何定義這個事件處理的實際樣本的高低,但我找不到一個。

的Global.asax

protected void Application_Start() 
{ 

    FederatedAuthentication.WSFederationAuthenticationModule.SecurityTokenReceived 
      += SessionAuthenticationModule_SessionSecurityTokenReceived; 
    //   ^^^ There is no applicable variable or member 
} 



void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e) 
{ 
      DateTime now = DateTime.UtcNow; 
      DateTime validFrom = e.SessionToken.ValidFrom; 
      DateTime validTo = e.SessionToken.ValidTo; 
      if ((now < validTo) && 
      (now > validFrom.AddMinutes((validTo.Minute - validFrom.Minute)/2)) 
      ) 
      { 
       SessionAuthenticationModule sam = sender as SessionAuthenticationModule; 
       e.SessionToken = sam.CreateSessionSecurityToken(
        e.SessionToken.ClaimsPrincipal, 
        e.SessionToken.Context, 
        now, 
        now.AddMinutes(2), 
        e.SessionToken.IsPersistent); 
       e.ReissueCookie = true; 
      } 
      else 
      { 
       //todo: WSFederationHelper.Instance.PassiveSignOutWhenExpired(e.SessionToken, this.Request.Url); 

       // this code from: http://stackoverflow.com/questions/5821351/how-to-set-sliding-expiration-in-my-mvc-app-that-uses-sts-wif-for-authenticati 

       var sessionAuthenticationModule = (SessionAuthenticationModule)sender; 

       sessionAuthenticationModule.DeleteSessionTokenCookie(); 

       e.Cancel = true; 
      } 
    } 

回答

9

我不認爲你需要的事件訂閱。在開始拆除subcription,只是使用

SessionAuthenticationModule_SessionSecurityTokenReceived

ASP.Net將連線,爲您服務。 (該模塊必須命名爲「SessionAuthenticationModule」,默認情況下)。

如果您在滑動會議的工作,該博客文章由維托裏奧是相當不錯的:http://blogs.msdn.com/b/vbertocci/archive/2010/06/16/warning-sliding-sessions-are-closer-than-they-appear.aspx

+5

簡單和像魅力一樣工作!我怎麼可以告訴大家,需要接線了事件和那些不之間的差異 – LamonteCristo

0

而是在Global.asax中定義它的,創建一個繼承SessionAuthenticationModule一個新的類:

public class CustomAuthenticationModule : SessionAuthenticationModule 
{ 
    public CustomAuthenticationModule : base() 
    { 
     this.SessionSecurityTokenReceived += new EventHandler<SessionSecurityTokenReceivedEventArgs>(CustomAuthenticationModule_SessionSecurityTokenReceived); 
    } 

    void CustomAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e) 
    { 
     // Your code 
    } 
} 

然後在你的web.config文件中,用你的新模塊替換默認的SessionAuthentication模塊:

<modules> 
    <add name="SessionAuthenticationModule" type="CustomAuthenticationModule" preCondition="managedHandler"/> 
</modules> 
+0

謝謝,我已經和一些配置最近掙扎,這救了我的一天(我沒有一個全球性的網頁),雖然它是不是特別在global.asax中,應該可能在其他線程中,但我無法在其他地方找到這條信息。 – Mochi