HttpModule 添加标头到请求
这看起来是一个简单的操作。
我们需要在我们的开发环境(在 XP/IIS 5 上运行)中向到达我们应用程序的每个 HttpRequest 添加一些标头。 (这是为了模拟我们在开发中没有的生产环境)。乍一看,这似乎是一个简单的 HttpModule,大致如下:
public class Dev_Sim: IHttpModule
{
public void Init(HttpApplication app)
{
app.BeginRequest += delegate { app.Context.Request.Headers.Add("UserName", "XYZZY"); };
}
public void Dispose(){}
}
但在尝试这样做时,我发现 Request 的 Headers 集合是只读的,并且 Add 方法失败并出现 OperationNotSupported 异常。
我花了几个小时在谷歌上研究这个问题,对于这个应该是相对简单的问题,我没有找到简单的答案。
有人有任何指点吗?
This seems like a simple operation.
We have a need in our development environment (running on XP/IIS 5) to add some headers into each HttpRequest arriving at our application. (This is to simulate a production environment that we don't have available in dev). At first blush, this seemed like a simple HttpModule, along the lines of:
public class Dev_Sim: IHttpModule
{
public void Init(HttpApplication app)
{
app.BeginRequest += delegate { app.Context.Request.Headers.Add("UserName", "XYZZY"); };
}
public void Dispose(){}
}
But on trying to do that, I find that the Headers collection of the Request is read-only, and the Add method fails with an OperationNotSupported exception.
Spending a couple hours researching this on Google, I've come up with no easy answer to what should be a relatively straight-forward problem.
Does anyone have any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,在同事的帮助和一些实验的帮助下,我发现这可以通过通过反射访问的一些受保护的属性和方法的帮助来完成:
这至少对我有用。
Okay, with the assistance of a co-worker and some experimentation, I found that this can be done with the assistance of some protected properties and methods accessed through reflection:
This works for me, at least.
您可以通过这种方式添加到标题。这是一种在请求进入身份验证序列之前向请求添加凭据信息的方法。
You can add to the Header this way. This is a way to add credential information to the request before it enter the authentication sequence.