Mag*_*pie 5 c# asp.net-mvc-4 asp.net-web-api
我正在使用新的Web Api测试版并希望返回
HttpResponseMessage<MyObject>(new MyObject{ MyMessage = "Go Away!" }, HttpStatusCode.Unauthorized)
来自我的一个ApiController动作.
表单身份验证劫持响应,崩溃并将错误添加"Cannot redirect after HTTP headers have been sent."到响应中.
像这样的常规抑制技术不适用于Web Api.
有人找到了解决这个问题的工作吗?
我看过这个论坛帖子,人们报告同样的问题,但那里的解决方案不适用于这种情况.建议的第一个解决方案使用常规抑制技术,该技术不适用于web api.第二个使用HttpMessageHandler在它到达控制器之前拦截请求,我希望控制器正常触发.
在查看之后DelegatingHandler我可以访问HttpResponseMessage但不知道如何处理它以阻止FormsAuth重定向.
我在使用Phil Haack 的 SuppressFormsAuthenticationRedirectModule时遇到了同样的问题
我设法通过清除服务器的错误来修复它,如下所示
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5505 次 |
| 最近记录: |