【问题标题】:W.I.F.: Setting IsSessionMode to true, can't seem to make it happenW.I.F.:将 IsSessionMode 设置为 true,似乎无法实现
【发布时间】:2011-12-11 04:07:12
【问题描述】:

我们在使用 Safari(和 Opera)时遇到问题,据我所知,FedAuth cookie 太大了。

有一个“巧妙的技巧”可以解决这个问题: “WIF RTM 向 SessionAuthenticationModule 添加了一个属性 IsSessionMode。当翻转为 true 时,IsSessionMode 具有确保 SessionSecurityToken 在整个会话期间保留在缓存中并生成仅包含会话标识符而不是会话本身的内容。”

我在 global.asax 中有这段代码:

void WSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender, Microsoft.IdentityModel.Web.SessionSecurityTokenCreatedEventArgs e)
{
    FederatedAuthentication.SessionAuthenticationModule.IsSessionMode = true;
}

问题,“FederatedAuthentication.SessionAuthenticationModule.IsSessionMode = true”永远不会运行......为什么?


将IsSessionMode设置为true是否与“PassiveSignInControl”有关?

MSDN Post

your-fedauth-cookies-on-a-diet-issessionmode-true.aspx

摘自《Programming Windows® Identity Foundation》一书:

“SAM 的一个有趣属性是 IsSessionMode。当设置为 true,IsSessionMode 具有存储大部分会话的效果 在服务器端令牌缓存上,而不是将所有内容写入 曲奇饼。 cookie 本身将只包含一个小上下文 标识符,将用于在 服务器。不幸的是,在这个版本的 92 Part II Windows Identity Developers 产品的 Identity Foundation 没有办法 从配置文件中设置 IsSessionMode。你可以通过一个 PassiveSignInControl 的属性,或在 global.asax 文件中作为 如下(与上面相同的代码)”

【问题讨论】:

  • 好的,读完这条评论后听起来好像我很久没有尝试过这个了,但事实并非如此。我认为每次用户登录时都会运行“WSFederationAuthenticationModule_SessionSecurityTokenCreated”,现在它以“随机”的方式运行并长时间刹车。它可能在更新 cookie 时正在运行。 “WSFederationAuthenticationModule_SessionSecurityTokenCreated”应该什么时候运行??

标签: c# web-applications wif


【解决方案1】:

需要注意的重要一点是如何处理 WSFederationAuthenticationModule 类的 SecurityTokenValidatedSessionSecurityTokenCreated 事件。

备选方案 1 — 在 global.asax 中自动连接事件(显式方法名称,无需手动连接到事件):

void WSFederationAuthenticationModule_SecurityTokenValidated(object sender, SecurityTokenValidatedEventArgs e)
{
    FederatedAuthentication.SessionAuthenticationModule.IsReferenceMode = true;
}

// or

void WSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender, SessionSecurityTokenCreatedEventArgs e)
{
    e.SessionToken.IsReferenceMode = true;
}

Alternative 2 — 手动方法连接到 global.asax 中的事件。关键是它不能在 Application_Start 中,而是在覆盖的 Init 中:

void Application_Start(object sender, EventArgs e)
{
    // Called only once on application start
    // This is not the right place to wire events for all HttpApplication instances
}

public override void Init()
{
    // Called for each HttpApplication instance
    FederatedAuthentication.WSFederationAuthenticationModule.SecurityTokenValidated += STV;
    FederatedAuthentication.WSFederationAuthenticationModule.SessionSecurityTokenCreated += SSTC;
}

void STV(object sender, SecurityTokenValidatedEventArgs e)
{
    FederatedAuthentication.SessionAuthenticationModule.IsReferenceMode = true;
}

// or

void SSTC(object sender, SessionSecurityTokenCreatedEventArgs e)
{
    e.SessionToken.IsReferenceMode = true;
}

【讨论】:

    【解决方案2】:

    旧线程,但我相信 SessionSecurityTokenCreated 是处理这个问题的正确事件——经过测试,它可以在“旧 WIF”和具有适当命名空间变体的 NET 4.5 下工作。

    void WSFederationAuthenticationModule_SessionSecurityTokenCreated(object sender, System.IdentityModel.Services.SessionSecurityTokenCreatedEventArgs e)
    {
        e.SessionToken.IsReferenceMode = true;
    }
    

    【讨论】:

    • msdn.microsoft.com/en-us/library/… 要在引用模式下操作,Microsoft 建议为 WSFederationAuthenticationModule.SessionSecurityTokenCreated 事件提供处理程序...在 SessionSecurityTokenCreatedEventArgs.SessionToken 属性中传递的令牌上设置 IsReferenceMode 属性。这将确保会话令牌在每个请求的引用模式下运行,并且优于仅设置 SessionAuthenticationModule.IsReferenceMode 属性...
    【解决方案3】:

    您好,试试这个:使用 SecurityTokenValidated 代替 SessionSecurityTokenCreated 事件

    在 global.ascx 中

    void WSFederationAuthenticationModule_SecurityTokenValidated(object sender, SecurityTokenValidatedEventArgs e) 
    {   
        FederatedAuthentication.SessionAuthenticationModule.IsSessionMode = true; 
    }
    

    查看来自Dominick Baier blog的评论

    【讨论】:

      【解决方案4】:

      您是否为SessionSecurityTokenCreated 事件注册了您的事件处理程序?

      FederatedAuthentication.WSFederationAuthenticationModule.SessionSecurityTokenCreated 
          += this.WSFederationAuthenticationModule_SessionSecurityTokenCreated;
      

      需要将此行添加到您的Global.asax 文件中的Application_Start 方法中。

      命名空间Microsoft.IdentityModel.Web中的FederatedAuthentication类。

      【讨论】:

      • 我们试过了,但我在 Application_Start 中没有它,因为:WSFederationAuthenticationModule 'Microsoft.IdentityModel.Web.FederatedAuthentication.WSFederationAuthenticationModule' threw an exception of type 'System.NullReferenceException' Microsoft.IdentityModel.Web.WSFederationAuthenticationModule {System.NullReferenceException}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      相关资源
      最近更新 更多