如何在使用 STS (WIF) 进行身份验证的 MVC 应用程序中设置滑动过期

发布于 2024-11-03 14:27:46 字数 842 浏览 1 评论 0原文

我们正在使用 STS 开发一个 MVC 应用程序。我们使用 WIF 工具创建了一个简单的 STS 应用程序进行开发。

我希望能够在我的令牌(在 RP 中)中设置滑动到期时间。

我看到代码像这里

不幸的是,这是事件处理程序,该示例虽然有帮助,但没有显示如何实现该处理程序!

在我的 global.asax, Application_Start() 中,我有:

sam = new SessionAuthenticationModule();
        sam.SessionSecurityTokenReceived += 
            new EventHandler<SessionSecurityTokenReceivedEventArgs>(sam_SessionSecurityTokenReceived);

(sam 是使用类范围定义的。)

我不确定这是否正确。由于 global.asax 中的调试问题,我不知道如何验证该事件是否被调用过。

是否有更完整的示例来说明如何捕获此事件?我以正确的方式处理这件事吗?

蒂亚!我很感激你的帮助! Rich

Edit - 好吧,我知道该事件没有被调用,因为我在处理程序中放置了除以零的代码,并且应用程序没有抛出异常。我通过 STS 登录,因此任何收到的令牌事件都应该被触发。

任何有关如何执行此操作的帮助将不胜感激。谢谢!

We are developing an MVC app using STS. We used the WIF tools to create a simple STS app for development.

I would like to be able to set a sliding expiration in my token (in the RP).

I see code like here.

Unfortunately, this is the event handler and the example, while helpful, doesn't show how to implement the handler!

In my global.asax, Application_Start() I have:

sam = new SessionAuthenticationModule();
        sam.SessionSecurityTokenReceived += 
            new EventHandler<SessionSecurityTokenReceivedEventArgs>(sam_SessionSecurityTokenReceived);

(sam is defined with a class scope.)

I'm not sure if this is correct. I do not know how to verify if the event was ever called because of debugging issues in global.asax.

Is there a more complete example somewhere of how to trap this event? Am I going about it the right way?

TIA! I appreciate the help!
Rich

Edit - well, I know that the event is not getting called because I put divide by zero code in the handler and the app did not throw an exception. I logged in thru my STS, so any token recieved event should have been fired.

Any help on how to do this would be greatly appreciated. thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2024-11-10 14:27:46

由于 WIF 仅允许固定长度的会话,因此需要重新颁发安全令牌,此时您可以将令牌的 IsValidTo 属性设置为您需要的任何值。

将其放入您的 global.asax 文件中:

protected void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e)
{
    var sessionToken = e.SessionToken;
    SymmetricSecurityKey symmetricSecurityKey = null;

    if (sessionToken.SecurityKeys != null)
        symmetricSecurityKey = sessionToken.SecurityKeys.OfType<SymmetricSecurityKey>().FirstOrDefault();

    Condition.Requires(symmetricSecurityKey, "symmetricSecurityKey").IsNotNull();

    if (sessionToken.ValidTo > DateTime.UtcNow)
    {
        var slidingExpiration = sessionToken.ValidTo - sessionToken.ValidFrom;

        e.SessionToken = new SessionSecurityToken(
                    sessionToken.ClaimsPrincipal,
                    sessionToken.ContextId,
                    sessionToken.Context,
                    sessionToken.EndpointId,
                    slidingExpiration,
                    symmetricSecurityKey);

        e.ReissueCookie = true;
    }
    else
    {
        var sessionAuthenticationModule = (SessionAuthenticationModule) sender;

        sessionAuthenticationModule.DeleteSessionTokenCookie();

        e.Cancel = true;
    }
}

来源:http://blogs.planbsoftware.co.nz /?p=5211

Since WIF only allows fixed length sessions, it requires reissuing the security token at which point you can set when the token IsValidTo property of the token to whatever you require.

Put this in your global.asax file:

protected void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e)
{
    var sessionToken = e.SessionToken;
    SymmetricSecurityKey symmetricSecurityKey = null;

    if (sessionToken.SecurityKeys != null)
        symmetricSecurityKey = sessionToken.SecurityKeys.OfType<SymmetricSecurityKey>().FirstOrDefault();

    Condition.Requires(symmetricSecurityKey, "symmetricSecurityKey").IsNotNull();

    if (sessionToken.ValidTo > DateTime.UtcNow)
    {
        var slidingExpiration = sessionToken.ValidTo - sessionToken.ValidFrom;

        e.SessionToken = new SessionSecurityToken(
                    sessionToken.ClaimsPrincipal,
                    sessionToken.ContextId,
                    sessionToken.Context,
                    sessionToken.EndpointId,
                    slidingExpiration,
                    symmetricSecurityKey);

        e.ReissueCookie = true;
    }
    else
    {
        var sessionAuthenticationModule = (SessionAuthenticationModule) sender;

        sessionAuthenticationModule.DeleteSessionTokenCookie();

        e.Cancel = true;
    }
}

Source: http://blogs.planbsoftware.co.nz/?p=5211

半寸时光 2024-11-10 14:27:46

虽然 bmeredith 的答案看起来完全有效,但有一件事很突出。

看起来令牌会根据每个请求进行更新,并且加密操作通常并不便宜。
我发现了一种类似但略有不同的方法,仅在 1/2 会话过去后才更新令牌。

我还喜欢使用 SessionAuthenticationModule 来创建令牌,这样我们就不必弄乱密钥。

http://www.cloudidentity。 com/blog/2013/05/08/sliding-sessions-for-wif-4-5/

void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender,

System.IdentityModel.Services.SessionSecurityTokenReceivedEventArgs e)
{ 
    DateTime now = DateTime.UtcNow;
    SessionSecurityToken sst = e.SessionToken;
    DateTime validFrom = sst.ValidFrom;
    DateTime validTo = sst.ValidTo; 
    if ((now < validTo) && (now > validFrom.AddMinutes( (validTo.Minute - validFrom.Minute) / 2)) ) 
    { 
        SessionAuthenticationModule sam = sender as SessionAuthenticationModule;
        e.SessionToken = sam.CreateSessionSecurityToken(sst.ClaimsPrincipal,
                                                        sst.Context,
                                                        now,
                                                        now.AddMinutes(2),
                                                        sst.IsPersistent); 
                                                        e.ReissueCookie = true; 
    }
}

While the answer by bmeredith looks perfectly valid, one thing sticks out.

It looks like the token is renewed upon every request, and cryptographic operations usually aren't cheap.
I found a similar but slightly different approach that only renews the token when ½ the session has passed.

Also I like the use of the SessionAuthenticationModule for creating the token, so we don't have to mess around with keys.

http://www.cloudidentity.com/blog/2013/05/08/sliding-sessions-for-wif-4-5/

void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender,

System.IdentityModel.Services.SessionSecurityTokenReceivedEventArgs e)
{ 
    DateTime now = DateTime.UtcNow;
    SessionSecurityToken sst = e.SessionToken;
    DateTime validFrom = sst.ValidFrom;
    DateTime validTo = sst.ValidTo; 
    if ((now < validTo) && (now > validFrom.AddMinutes( (validTo.Minute - validFrom.Minute) / 2)) ) 
    { 
        SessionAuthenticationModule sam = sender as SessionAuthenticationModule;
        e.SessionToken = sam.CreateSessionSecurityToken(sst.ClaimsPrincipal,
                                                        sst.Context,
                                                        now,
                                                        now.AddMinutes(2),
                                                        sst.IsPersistent); 
                                                        e.ReissueCookie = true; 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文