首页 > 解决方案 > 302 登录重定向后 IE 删除的 URL 片段

问题描述

我们在应用程序中使用SiteMinder身份验证。

如果应用程序用户尝试https://ourapp.com/myapp/#/pending/requests通过直接 URL 或通过书签 URL 导航到我们应用程序中的特定页面,SiteMinder将重定向到login page通过 302 重定向,类似于http://ourapp.com/login?redirect=https%3A%2F%2Fourapp.com%2Fmyapp%2F#/pending/requests要求用户在登录表单中输入凭据。成功认证后,用户应该被重定向到我们的应用程序并登陆请求的页面(/pending/requests)。

它在Chrome和中工作得非常好Firefox。当涉及到 IE 时,它会登陆https://ourapp.com/myapp/#/home(默认登陆页面)而不是https://ourapp.com/myapp/#/pending/requests.

我尝试google search results了我们的应用程序代码中提供的各种解决方案,例如,

// setting location back
window.location = window.location;
// setting location hash back
window.location.hash = window.location.hash;

虽然这个问答非常有道理,

我还想preserve the URL hash fragment in IE even it's 3xx redirect满足我的要求……!?

标签: internet-explorerredirectsingle-page-applicationhttp-status-code-302siteminder

解决方案


回答我自己的问题

我发现在成功验证后,SiteMinder正在302 redirection使用名称类似于value/myapp/. 下面的示例表格without hash fragmentredirect

登录表单示例

由于redirect隐藏变量只包含/myapp/不包含散列片段并且它是 302 重定向,因此即使在进入我们的应用程序之前,IE 也会自动删除散列片段,并且无论我们在应用程序代码中尝试的任何解决方案都没有奏效。

IE 仅重定向到/myapp/,它正在登陆我们应用程序的默认主页https://ourapp.com/myapp/#/home

浪费了将近一天的时间来弄清楚这种行为。

解决方案是:

通过附加现有值更改了登录表单隐藏变量 ( redirect)的值以保存散列片段。window.location.hash类似于下面的代码

$(function () {
  var $redirect = $('input[name="redirect"]');
  $redirect.val($redirect.val() + window.location.hash);
});

在此更改之后,redirect隐藏变量将用户请求的 URL 值存储为/myapp/#/pending/requests并将SiteMinder其重定向到/myapp/#/pending/requestsIE。

上述解决方案在所有三种浏览器中都运行良好Chrome, Firefox and IE

感谢@AlexFord 对此问题的详细解释并提供了解决方案


推荐阅读