WIF安全令牌缓存

Jef*_*eff 12 wif

我有一个网站是我们基于WIF的自定义STS的依赖方.我们最近实现了一个安全令牌缓存,如下所述:Azure/web-farm ready SecurityTokenCache.我们的实现与该链接中描述的实现之间的主要区别在于,我们使用Azure AppFabric缓存作为持久缓存的后备存储,而不是表存储.这有助于减轻我们在某些浏览器上的令牌截断问题,但引入了一个新问题(我们看到截断问题主要是在除了fedauth cookie之外还有谷歌分析+防伪cookie的页面上).我们现在每天收到几千次以下异常:

System.IdentityModel.Tokens.SecurityTokenException
ID4243: Could not create a SecurityToken. A token was not found in the token cache and no cookie was found in the context.

System.IdentityModel.Tokens.SecurityTokenException: ID4243: Could not create a       SecurityToken. A token was not found in the token cache and no cookie was found in the context.
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver)
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Run Code Online (Sandbox Code Playgroud)

此异常似乎发生在重定向循环中,因此我们将在1-2分钟的时间跨度内看到数百个异常.

在研究异常时,我无法找到任何有用的信息.到目前为止唯一有希望的金块是有人提到它可能与会话之前到期的缓存对象有关.

我们无法在内部重现问题,只知道它存在,因为数千个条目填满了我们的Elmah表.非常感谢任何帮助或见解.

我们推出了我们认为可能有助于解决问题的方法(下面的代码),但它没有效果:

HttpContext.Current.Response.Cookies.Remove("FedAuth");
WSFederationAuthenticationModule authModule = FederatedAuthentication.WSFederationAuthenticationModule;
string signoutUrl = (WSFederationAuthenticationModule.GetFederationPassiveSignOutUrl(authModule.Issuer, authModule.Realm, null));
Response.Redirect(signoutUrl);
Run Code Online (Sandbox Code Playgroud)

Len*_*nox 6

我有一个MVC单页面应用程序作为依赖方使用WSO2 4.5作为IDP并得到相同的错误 - "System.IdentityModel.Tokens.SecurityTokenException ID4243:无法创建SecurityToken.在令牌缓存中找不到令牌在上下文中没有找到cookie......."搜索并找到了下面由思考结合的Brock Allen发表的声明.

当浏览器发送包含用户声明的cookie时会抛出此异常,但无法执行某些处理(密钥已更改,因此无法验证令牌或使用服务器端缓存和缓存)是空的).最终用户无法对此做很多事情,他们将继续得到错误,因为浏览器将继续发送cookie.

全文:http://brockallen.com/2012/10/22/dealing-with-session-token-exceptions-with-wif-in-asp-net/

在同一篇文章中,他提供了以下代码片段,解决了我的问题.在Global.asax中:

void Application_OnError()
{
    var ex = Context.Error;
    if (ex is SecurityTokenException)
    {
        Context.ClearError();
        if (FederatedAuthentication.SessionAuthenticationModule != null)
        {
            FederatedAuthentication.SessionAuthenticationModule.SignOut();
        }
        Response.Redirect("~/");
    }
}
Run Code Online (Sandbox Code Playgroud)


Eug*_*ace 0

如果令牌中的声明数量或令牌的总大小太大而无法在 cookie 中来回传输,则应执行此操作。如果情况并非如此,请简化您的解决方案,仅使用使用 cookie 的默认设置。但是,您应该使用基于证书的 cookie 加密,因此它仍然是“农场友好的”。默认情况下,WIF 将使用具有机器关联性的 DPAPI 进行加密。