当原始URL具有查询字符串时,表单登录重定向失败

时间:2011-08-09 15:45:24

标签: asp.net forms-authentication

我们在IIS 7.5下的ASP.Net 3.5 Web应用程序中使用表单身份验证。我们发现当源页面有这样的查询字符串时:

http://server/dir/page.aspx?func=add

登录页面的重定向看起来会将我发送到:

http://server/dir/page.aspx%3ffunc%3dadd

即使该网址解析为http://server/dir/page.aspx?func=add IIS抱怨

HTTP Error 400.0 - Bad Request
ASP.NET detected invalid characters in the URL.

我的Google-fu在研究这种行为方面让我失望。有没有人有解决方案或指向更多信息的指针?

更新

问题答案:

是的,我们正在调用FormsAuthentication.RedirectFromLoginPage。

我看了this article,这似乎是针对IIs7和Win28的。在查看引用的修补文件时,我的约会时间大约为3年。虽然我确实添加了引用的注册表并重新启动了该框;仍然会发生。

2 个答案:

答案 0 :(得分:0)

尝试将以下内容添加到您的web.config中:

<system.web>
   <httpRuntime 
        requestValidationMode="2.0"
        requestPathInvalidCharacters="&lt;,&gt;,*,%,:,&amp;,\"
        relaxedUrlToFileSystemMapping="true"
    />       
</system.web>

<system.webServer>
    <security>
        <requestFiltering allowDoubleEscaping="true" />
    </security>
</system.webServer>

答案 1 :(得分:0)

在深入研究之后因为我没有找到答案而且我已经惹恼了我,因此我找到了答案。

事实证明,我错了。在我们调用FormsAuthentication.RedirectFromLoginPage的地方是死代码,并且在身份验证服务告诉我们尝试成功后,在客户端处理重定向。

这段代码失败了:

var queryStringParts = window.location.search.split('=');
var targetPath = queryStringParts[1];
targetPath = targetPath.replace(/(%2f)/g,'/'); // <-- Here's where we weren't doing enough

我已经编写了一个实用工具方法来获取查询字符串值并正确处理它。

function getQueryStringValue(name, defaultValue) {
    var value = defaultValue;
    var queryString = window.location.search;
    var startPos = queryString.indexOf(name);

    if (startPos > -1) {
        startPos += name.length + 1
        var endPos = queryString.indexOf('&', startPos);
        if (endPos == -1) {
            // Incase there is no ampersand (&) after the query string value 
            // we're after we go to the last index + 1
            endPos = queryString.length;
        }
        value = unescape(queryString.substring(startPos, endPos));
    }
    return value;
}