2016-07-22 58 views
0

我正在为ASP.NET应用程序编写基本身份验证Http筛选器。如何将头添加到HttpActionContext响应而不使管道短路

作为身份验证的一部分,我需要在响应中添加一个头。当我的过滤器被调用时,actioncontext.Response为null。如果我创建了响应,我可以添加我的头文件,但是它会使管道短路,并且实际的控制器功能永远不会被调用。

我应该用不同的方式来实现我的授权吗?

回答

0

请使用全球AuthenticateRequest和PreSendRequestHeaders事件来更新响应:

public class Global : HttpApplication 
{ 
    // The AuthenticateRequest event signals that the configured authentication mechanism has authenticated the current request. 
    // Subscribing to the AuthenticateRequest event ensures that the request will be authenticated before processing the attached module or event handler. 
    public void Application_AuthenticateRequest(object sender, EventArgs e) 
    { 
     // your authenticate code 
    } 

    public void Application_PreSendRequestHeaders(object sender, EventArgs e) 
    { 
     // your add heard code 
    } 
} 
+0

感谢您的@Fan。我确实调查了这个想法,但它看起来是错误的。我已经成功地连接了一个动作过滤器,该过滤器将在途中生成标题,并将其放置在出路的响应中。它似乎是在当前版本的ASP.NET中执行此操作的“正确”方式。 – Neil

相关问题