【问题标题】:How do I handle the event SessionSecurityTokenReceived in Global.asax?如何处理 Global.asax 中的 SessionSecurityTokenReceived 事件?
【发布时间】:2011-12-28 20:00:54
【问题描述】:

我正在尝试在 WIF 中设置滑动会话,需要处理 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;
            }
  } 

【问题讨论】:

    标签: events global-asax wif windows-identity


    【解决方案1】:

    我认为您不需要事件订阅。在开始时删除订阅并使用

    SessionAuthenticationModule_SessionSecurityTokenReceived

    ASP.Net 将为您连接。 (模块必须命名为“SessionAuthenticationModule”,默认为)。

    如果您正在研究滑动会话,Vittorio 的这篇博文非常好:http://blogs.msdn.com/b/vbertocci/archive/2010/06/16/warning-sliding-sessions-are-closer-than-they-appear.aspx

    【讨论】:

    • 简单,像魅力一样工作!如何区分需要连接的事件和不需要连接的事件
    【解决方案2】:

    不要在 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>
    

    【讨论】:

    • 谢谢,我最近一直在为一些配置而苦苦挣扎,这节省了我的时间(我没有全局页面),虽然它不是特别在 global.asax 中,应该在某些其他线程,但我无法在其他地方找到这条信息。
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2012-05-20
    • 2012-09-17
    相关资源
    最近更新 更多