2011-11-14 36 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