2013-04-09 135 views
1

最近,我已经开始在现有的Web应用程序上使用基于声明的身份验证。由于该应用程序更利用jQuery &,AJAX函数,我不得不改变处理程序不要尝试重定向XmlHTTPRequests,而是返回一个403状态,这更容易处理。如何避免“SamlAssertion.NotOnOrOffer不满意”错误

这里是FederatedAuthentication.WSFederationAuthenticationModule.AuthorizationFailed事件hanlder:

protected void WSFederationAuthenticationModule_AuthorizationFailed(object sender, AuthorizationFailedEventArgs e) 
    { 
     //WSFederationAuthenticationModule sam = (WSFederationAuthenticationModule)sender; 

     HttpContext context = HttpContext.Current; 
     HttpRequest req = context.Request; 
     HttpResponse resp = context.Response; 

     if (req == null || resp == null) return; 

     if ((resp.StatusCode == 302 || resp.StatusCode == 401) && req.Headers["X-Requested-With"] == "XMLHttpRequest") 
     { 
      resp.StatusCode = 403; 
      e.RedirectToIdentityProvider = false; 
     } 

    } 

我已经实现了AJAX调用和处理响应以下模式:

$.ajax({ 
cache: false, 
data: $.param(o), 
dataType: "xml", 
url: "AJAXCall.ashx", 
success: function (data) 
{ 
    // Success handler 
}, 
error: function (XMLHttpRequest, textStatus, responseText) 
{ 
    if (XMLHttpRequest.status == 403) 
    { 
     var retVal = window.showModalDialog("Session.aspx", "", "dialogHeight: 250px; dialogWidth: 250px; edge: Raised; center: Yes; resizable: Yes; status: Yes;"); 
     if (retVal) 
     { 
      // Succesful session renewal handler 
     } 
     else 
     { 
      // Session renewal failure handler 
     } 
    } 
    else 
    { 
     // Other errors handler 
    } 
} 
}); 

的 'Session.aspx' 基本上关闭模式对话框,如果成功重定向到身份提供程序并返回,则返回值为true。

但我的问题是,我得到以下错误:

"ID4223: The SamlSecurityToken is rejected because the SamlAssertion.NotOnOrAfter condition is not satisfied."

这是上调用模拟当前应用程序的用户,显然前一交易日的令牌仍然存在的一个子系统。我在我的应用程序的web.config以下设置:

<federatedAuthentication> 
<wsFederation passiveRedirectEnabled="true" persistentCookiesOnPassiveRedirects="true" issuer="https://adfs.example.com/adfs/ls/" realm="https://www.example.com:449/" requireHttps="true" /> 
<cookieHandler requireSsl="true" /> 

如何避免这个错误?任何帮助将不胜感激。

+0

请求对象已经有一个方法来检测请求是否是ajax请求。这将意味着你不必显式地检查标题。尝试context.Request.IsAjaxRequest() – 2015-10-30 10:14:28

回答

2

以下FederatedAuthentication.WSFederationAuthenticationModule.SignInError事件处理方法整理出来的问题:

protected void WSFederationAuthenticationModule_SignInError(object sender, ErrorEventArgs e) 
    { 
     // handle an intermittent error that most often occurs if you are redirected to the STS after a session expired, 
     // and the user clicks back on the browser - second error very uncommon but this should fix 
     if (e.Exception.Message.StartsWith("ID4148") || 
      e.Exception.Message.StartsWith("ID4243") || 
      e.Exception.Message.StartsWith("ID4223")) 
     { 
      FederatedAuthentication.SessionAuthenticationModule.DeleteSessionTokenCookie(); 
      e.Cancel = true; 
     } 
    } 

它会删除会话令牌的Cookie是坚持,用户被重定向到STS服务即使在后会话已过期。

+0

我似乎仍然间歇性地得到这个问题。一旦找到合适的解决方案,我会更新我的答案。 – Raybiez 2013-04-10 10:11:09

+0

该问题与使用线程标识的高速缓存副本的子系统相关,从而导致错误。 – Raybiez 2013-04-18 08:07:58