5

WIF でスライディング セッションを設定しようとしており、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;
            }
  } 
4

2 に答える 2

9

イベントのサブスクリプションは必要ないと思います。開始時にサブスクリプションを削除し、そのまま使用する

SessionAuthenticationModule_SessionSecurityTokenReceived

ASP.Net がそれを配線します。(モジュールには「SessionAuthenticationModule」という名前を付ける必要があり、デフォルトではそうです)。

スライディング セッションに取り組んでいる場合は、Vittorio による次のブログ投稿が非常に役に立ちます。 -they-appear.aspx

于 2011-11-14T20:04:50.527 に答える
0

Global.asaxで定義する代わりに、SessionAuthenticationModuleを継承する新しいクラスを作成します。

public class CustomAuthenticationModule : SessionAuthenticationModule
{
   public CustomAuthenticationModule()
   {
      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>
于 2011-11-14T20:07:00.667 に答える