IIS的奇怪的AJAX重定向401问题

时间:2015-04-23 09:55:26

标签: ajax asp.net-mvc iis

我应该删除此问题吗? 我弄清楚问题是什么,它不是IIS ...请看下面的答案结果。

原始问题 我正在使用ASP.Net MVC应用程序并遇到一个奇怪的问题,即URL重写重定向和AJAX请求。

我已将以下重写规则添加到根网站中的Web.config。

    unique(df[c('a', 'b', 'c')])

如果我在配置中使用partial<rewrite> <rules> <rule name="Account" stopProcessing="true"> <match url="^SubApp/Account/(.*)$" /> <action type="Redirect" url="Account/{R:1}" redirectType="Found" /> </rule> </rules> </rewrite> Permanent但是在Temporary IIS错误页面失败时,似乎一切正常。

当我通过浏览器向触发此规则的操作发出正常的GET请求时,例如redirectType然后我找到302 Found,并将location头设置为预期的URL HTTP Error 401.0 - Unauthorized,并呈现相应的页面。

但是,当我通过JQuery的AJAX(即https://some-site/SubApp/Account/Settings)发出GET请求时,返回的响应状态代码为https://some-site/Account/Settings,但它仍然具有相应的位置标头。

响应内容是标准的IIS $.get('https://some-site/SubApp/Account/Settings')错误页面。

奇怪的是,如果我在配置中使用401 UnauthorizedHTTP Error 401.0 - Unauthorized重定向类型,但只有Permanent失败,那么一切似乎都能正常工作。

Temporary是一个单独的应用程序,位于Found的根网站下方。

发生了什么?

截图

/SubApp Permanent Redirect

/ Found Redirect

redirectType="Permanent" Temporary Redirect

正如您从屏幕截图中看到的,唯一的区别是redirectType="Found"中指定的redirectType="Temporary"

正如您所看到的那样,重定向正在按预期发生,但redirectType重定向类型除外,我希望将Web.config响应重定向到与其他重定向相同的网址。

2 个答案:

答案 0 :(得分:5)

啊啊,你知道当你暂时没有想到什么东西而且你会突然受到启发......好吧它发生在昨晚我发现这个小金币到位了&#34;修复& #34;当认证失败时,MVC坚持重定向AJAX请求......

protected void Application_EndRequest()
{
    var context = new HttpContextWrapper(Context);
    // MVC retuns a 302 for unauthorized ajax requests so alter to request status to be a 401
    if (context.Response.StatusCode == 302 && context.Request.IsAjaxRequest() && !context.Request.IsAuthenticated)
    {   
        context.Response.Clear();
        context.Response.StatusCode = 401;
    }
}

而且,毫无疑问,context.Request.IsAuthenticated始终为假,因为它似乎会被重定向重置。

通过Branislav Abadjimarinov's blog post对该主题的一些帮助,对此进行了更新。

protected void Application_EndRequest()
{
    var context = new HttpContextWrapper(Context);
    // MVC returns a 302 for unauthorized ajax requests so alter to request status to be a 401

    if (context.Response.StatusCode == 302 && context.Request.IsAjaxRequest())
    {
        //Unfortunately the redirect also clears the results of any authentication
        //Try to manually authenticate the user...
        var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            if (authTicket != null && !authTicket.Expired)
            {
                var roles = authTicket.UserData.Split(',');
                HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(authTicket), roles);
            }
        }

        if (!context.Request.IsAuthenticated)
        {
            context.Response.Clear();
            context.Response.StatusCode = 401;
        }

    }
}

这一切都按预期工作。

我应该删除这个问题吗?

答案 1 :(得分:0)

看看这个Cannot handle 302 redirect in ajax and why? [duplicate],看起来Web浏览器看到了Found-302并对其执行了操作。

相关问题