我有一个网站,这是我们的WIF为基础的自定义STS的依赖方。我们最近实现了一个安全令牌缓存,如下所述:Azure/web-farm ready SecurityTokenCache。我们的实现与该链接中描述的实现之间的主要区别是,我们使用Azure AppFabric缓存作为持久缓存的后备存储,而不是表存储。这有助于减轻我们在某些浏览器上的令牌截断问题,但也带来了新的问题(我们看到截断问题主要出现在除了fedauth cookie之外还有google analytics +防伪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)这个异常似乎是在重定向循环中发生的,所以我们将在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);发布于 2014-05-09 23:22:13
我有一个作为依赖方的MVC单页应用程序,使用Cookie4.5作为IDP,并收到相同的错误- "System.IdentityModel.Tokens.SecurityTokenException ID4243:无法创建Cookie.在令牌缓存中找不到令牌,在上下文中也找不到SecurityToken ...“做了一个搜索,找到了下面由Thinktecture成名的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("~/");
}
}发布于 2013-03-24 23:09:43
这个问题是由缓存SessionSecurityToken引起的。缓存目标位于应用程序池的本地域中,因此当.NET需要内存时,它将自动被清除。最好的解决方案是两个,为了安全而取消缓存,或者实现自己的缓存子系统。
解决方案1
Windows Server memcached的AppFabric -一个分布式内存对象缓存系统
解决方案2
var sessionSecurityToken = new SessionSecurityToken(principal, TimeSpan.FromHours(Convert.ToInt32(System.Web.Configuration.WebConfigurationManager.AppSettings["SessionSecurityTokenLifeTime"])))
{
IsPersistent = false, // Make persistent
IsReferenceMode = true // Cache on server
};
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionSecurityToken);发布于 2012-03-29 04:52:58
如果您浏览到一个新启动的应用程序,而您的浏览器仍然持有以前会话中的cookie,我们就会看到这个确切的错误。由于这些cookie是会话cookie,因此修复方法是关闭所有浏览器窗口,然后再次浏览到应用程序。
据我所知,我们的应用程序是一个使用WIF重定向到AD FS的“普通”web应用程序,没有任何特殊的安全令牌缓存内容。但是,我们对WIF使用了“会话模式”(例如"Your FedAuth Cookies on a Diet: IsSessionMode=true"),这使得WIF变得小得多。
https://stackoverflow.com/questions/9793900
复制相似问题