简体   繁体   English

“在发送HTTP标头后无法重定向。”使用HttpStatusCode.Unauthorized返回HttpResponseMessage时

[英]“Cannot redirect after HTTP headers have been sent.” When returning HttpResponseMessage with HttpStatusCode.Unauthorized

I'm using the new Web Api beta and wish to return 我正在使用新的Web Api测试版并希望返回

HttpResponseMessage<MyObject>(new MyObject{ MyMessage = "Go Away!" }, HttpStatusCode.Unauthorized)

from one of my ApiController actions. 来自我的一个ApiController动作。

Forms Authentication hijacks the response, crashes and adds the error "Cannot redirect after HTTP headers have been sent." 表单身份验证劫持响应,崩溃并添加错误"Cannot redirect after HTTP headers have been sent." and it's html to the response. 这是回复的HTML。

Normal suppression techniques like this don't work with Web Api. 这样的常规抑制技术不适用于Web Api。

Has anyone found a work around to this? 有人找到了解决这个问题的工作吗?

I've looked at this forum post where people report the same problem but the solutions there don't work for this case. 我看过这个论坛帖子 ,人们报告同样的问题,但那里的解决方案不适用于这种情况。 The first solution suggested uses the normal suppression technique which doesn't work for web api. 建议的第一个解决方案使用常规抑制技术,该技术不适用于web api。 The second uses a HttpMessageHandler to intercept the request before it gets to the controller, I want the controller to fire as normal. 第二个使用HttpMessageHandler在它到达控制器之前拦截请求,我希望控制器正常触发。

After looking into the DelegatingHandler I can get access to the HttpResponseMessage but have no idea what to do with it to stop FormsAuth from redirecting. 在查看DelegatingHandler我可以访问HttpResponseMessage但不知道如何处理它以阻止FormsAuth重定向。

I faced the same issue when using the Phil Haack's SuppressFormsAuthenticationRedirectModule 使用Phil Haack的SuppressFormsAuthenticationRedirectModule时我遇到了同样的问题

I managed to fix it in my case by clearing the server's error as shown below 我设法通过清除服务器的错误来修复它,如下所示

private void OnEndRequest(object source, EventArgs args)
{
    var context = (HttpApplication)source;
    var response = context.Response;

    if (context.Context.Items.Contains(SuppressAuthenticationKey))
    {
        context.Server.ClearError(); //Clearing server error

        response.TrySkipIisCustomErrors = true;
        response.ClearContent();
        response.StatusCode = 401;
        response.RedirectLocation = null;
    }
}

Hi as this question still seems to be open I thought I would point out that the latest MVC 4 RC release now appears to have the Forms redirect disabled by default. 嗨,因为这个问题似乎仍然是开放的我想我会指出最新的MVC 4 RC版本现在似乎默认禁用了表单重定向。 I did some digging whilst replying to this question Prevent FormsAuthenticationModule of intercepting ASP.NET Web API responses 我做了一些挖掘,同时回复了这个问题, 阻止了阻止ASP.NET Web API响应的FormsAuthenticationModule

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何下载多个文件? 获取“发送HTTP标头后服务器无法清除标头。” - How to download multiple files? Getting “Server cannot clear headers after HTTP headers have been sent.” 发送 HTTP 标头后,服务器无法设置内容类型。 使用 JsonResult 调用 FileStreamResult 方法时 - Server cannot set content type after HTTP headers have been sent. When using a JsonResult to call a FileStreamResult method Html.AntiForgeryToken()引发错误“服务器在发送HTTP标头后无法附加标头。” - Html.AntiForgeryToken() throws error “Server cannot append header after HTTP headers have been sent.” 发送HTTP标头后,服务器无法附加标头。 C#弹出窗口关闭 - Server cannot append header after HTTP headers have been sent. C# popup close ActionFilter - 发送 HTTP 标头后无法重定向 - ActionFilter - Cannot redirect after HTTP headers have been sent 发送HTTP标头后无法重定向 - Cannot redirect after HTTP headers have been sent 在 HttpStatusCode.Unauthorized 之后使用 Polly 重试 - Using Polly to retry after HttpStatusCode.Unauthorized ASP.NET 3.5,googlebot,301重定向“在发送HTTP标头后无法重定向” - ASP.NET 3.5, googlebot, 301 Redirect “Cannot redirect after HTTP headers have been sent” 发送HTTP标头后,服务器无法清除标头 - Server cannot clear headers after HTTP headers have been sent HTTPS重定向导致错误“在发送HTTP标头后,服务器无法附加标头” - HTTPS Redirect Causing Error “Server cannot append header after HTTP headers have been sent”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM