Setting HTTP_X_FORWARDED_FOR server variable in cl

2019-08-13 11:49发布

问题:

I need to set the HTTP_X_FORWARDED_FOR value in the Request.ServerVariables collection. I'm trying with Fiddler (see this article).

So I set up a custom HTTP request:

GET http://myhost/ HTTP/1.1 http_x_forwarded_for: my.fake.ip.1, my.fake.ip.2 Host: myhost.

The request is correctly executed.

But when I debug the code, the Request.ServerVariables("HTTP_X_FORWARDED_FOR") is empty. Where am I wrong? Is Fiddler the correct tool?

回答1:

The header you are sending should be x-forwarded-for. The Request.ServerVariables are exactly what it says server variables. It does not just contain the http headers sent in the request.

When placing the header values into server variables IIS creates a name for the variable that is based on the header name but is not the name verbatim. It converts all characters to uppercase, replaces all - with _ and adds the prefix of HTTP_ to header name to create a server variable name. The prefix is added to prevent arbitary headers aliasing other fixed server variable names.