ADFS经过身份验证的会话到期时,XHR请求失败

时间:2014-10-28 13:35:33

标签: xmlhttprequest adfs ws-federation

我们有一个站点,它依赖于使用Active Directory联合身份验证服务(ADFS)和WSFederationAuthenticationModule的联合身份验证。

该网站还使用了一组针对用户交互触发的XHR请求。一个特定示例是下拉菜单,其允许用户模仿其他用户。另一个是站点搜索页面上的分面功能。

问题是当用户在页面上保持非活动状态一段时间时会话到期。例如,当用户点击链接时,在正常的HTTP请求中,这不是问题。会话到期后,用户将被重定向到STS,并在用户没有注意到的情况下立即返回 - 它的发生得足够快。

但XHR请求失败。控制台中的实际错误消息是:

XMLHttpRequest cannot load https://adfs.contoso.com/adfs/ls/... 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'https://www.example.com' is therefore not allowed access.

似乎XHR请求无法重定向到STS并随后返回到依赖方,就像常规HTTP请求一样,因为这会导致CORS问题。

这自然会依赖于特定的javascript打破页面上的所有元素。用户解决此问题的唯一方法是在出现问题时刷新页面。

解决这个问题的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

将其添加到ADFS ls文件夹中的Web配置,或者如果您使用ADFS ls文件夹,则将其添加到ADFS代理上。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

REF: http://enable-cors.org/server_iis7.html

答案 1 :(得分:0)

唯一对我有用的解决方案是Pinpont在这个答案中提供的解决方案:https://stackoverflow.com/a/28631956/6299975

这就是我实现滑动过期的方法。

app.UseCookieAuthentication(new CookieAuthenticationOptions() 
{ 
    SlidingExpiration = false 
});
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
   {
      MetadataAddress = xxxxxxx,
      Wtrealm = xxxxxx,
      UseTokenLifetime = false,
   }

);

CookieAuthenticationOptions中的

SlidingExpiration = false

WsFederationAuthenticationOptions中的

UseTokenLifetime = false