2011-11-14 11 views
5

私はWIFでスライドセッションを設定しようとしており、SessionSecurityTokenReceivedを処理する必要があります。Global.asaxでSessionSecurityTokenReceivedイベントを処理するにはどうすればよいですか?

私はここで何かばかげたことをしていると確信しています...しかし、VS2010は、以下に示す場所でThere is no applicable variable or memberということを教えてくれます。誰かが私を正しい方向に向けることができますか?私は、このイベントの処理を定義する方法の実際のサンプルについて、高値と低値を検索しましたが、1つを見つけることはできません。

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

私は、イベントサブスクリプションを必要とは思いません。 ASP.Netはあなたのためにそれを配線します起動時にsubcriptionを削除し、ちょうど

SessionAuthenticationModule_SessionSecurityTokenReceived

を使用しています。 (モジュールの名前は "SessionAuthenticationModule"でなければなりません。デフォルトではこのモジュールが使用されています)。

あなたがスライディングセッションで作業している場合は、ヴィットリオによって、このブログの記事はかなり良いです:代わりにGlobal.asaxの中でそれを定義するhttp://blogs.msdn.com/b/vbertocci/archive/2010/06/16/warning-sliding-sessions-are-closer-than-they-appear.aspx

+5

簡単で魅力的です!配線が必要なイベントとそうでないイベントの違いを教えてください – LamonteCristo

0

、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

関連する問題