8

I've searched everywhere and nowhere does it mention where this key is defined, or similar ones such as HTTP_X_FORWARDED_FOR, REMOTE_ADDR etc.

MSDN documentation doesn't mention anything useful. The only thing close to useful I came about was some stackoverflow questions (this and this), along with this short blog post.

None of these sadly address my current question - from where does all these dictionary keys come from? Where is their specification, so that one knows they exist and learn to utilize them, by seeing the contents they hold?

EDIT: I'm using .NET Framework 4.6.0, where System.Net.Http's version is 4.0.0.0. To get the client IP, I'm doing the following:

public string GetSomeIp(HttpRequestMessage request)
{
    HttpContextWrapper context = 
       request.Properties["MS_HttpContext"] as HttpContextWrapper;

    return (context == null) ? 
       string.Empty : (context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
       ?? context.Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
}

I'd like to find the documentation, which explains what MS_HttpContext does/holds in detail, as well as REMOTE_ADDR, HTTP_X_FORWADED_FOR, and where they are defined, so I can see all the other keys and more in detail of their implementation/proper usage.

I'm aware of the following server variables, but the keys used here are not mentioned there. (except REMOTE_ADDR)

Mikhail
  • 9,186
  • 4
  • 33
  • 49
SpiritBob
  • 2,355
  • 3
  • 24
  • 62
  • Search Reference Source : https://referencesource.microsoft.com/ – jdweng Aug 22 '19 at 10:37
  • @jdweng nothing useful pops up when I search for "MS_HttpContext". – SpiritBob Aug 22 '19 at 10:39
  • Try : _httpcontext – jdweng Aug 22 '19 at 10:42
  • @jdweng I get some results, but I still can't see where these keys might be defined or explained. Are they part of Owin? – SpiritBob Aug 22 '19 at 10:44
  • You have to go back one more step to httpcontextBase – jdweng Aug 22 '19 at 10:50
  • @jdweng I've hit https://referencesource.microsoft.com/#System.Web/Abstractions/HttpContextBase.cs,91a04cddc33c5bea,references. If you know where I can find them, please post the link! – SpiritBob Aug 22 '19 at 10:53
  • You found the page were nothing is implemented. He is the one where they get the cookies : https://referencesource.microsoft.com/#System.Web/WebSockets/AspNetWebSocketContextImpl.cs,d8ae2223cdc06a24 – jdweng Aug 22 '19 at 11:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198357/discussion-between-spiritbob-and-jdweng). – SpiritBob Aug 23 '19 at 09:02
  • I've added new info about MS_HttpContext to the beginning of my answer. – Mikhail Aug 27 '19 at 17:19
  • 1
    MS_HttpContext is a hack people use to get HttpContext within a WebAPI application. It seems to be an implementation detail, I don't believe it will be documented ever. Everything else, as @Mikhail already answered, are IIS server variables, and they are described in the link you provided. – johnnyjob Aug 27 '19 at 22:56

1 Answers1

7

I'd like to find the documentation, which explains what MS_HttpContext does/holds in detail

The System.Web.Http.* assemblies seem not described on the https://referencesource.microsoft.com/.

However, all these projects are now hosted on the GitHub as Open? Source https://github.com/aspnet/AspNetWebStack. So far, I assume that this constant/key is used within a routine that assigns the System.Web -> HttpContext to System.Web.Http -> Request.

https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Http.WebHost/HttpRequestMessageExtensions.cs#L11-L44

Another occurrences are related to CORS and Tests. You can clone this repo and search for the "MS_HttpContext" occurrences in depth in you are looking for details. However, I am not sure about its documentation.

where this key is defined, or similar ones such as HTTP_X_FORWARDED_FOR, REMOTE_ADDR etc.

from where does all these dictionary keys come from?

These request's properties (aka Server Variables) are created (almost duplicated) from the corresponding headers sent by a client here (applicable for HttpContext - ASP.NET WebForms/MVC):

https://referencesource.microsoft.com/#System.Web/HttpRequest.cs,dc76880a3cfd0009

https://referencesource.microsoft.com/#System.Web/HttpRequest.cs,639

By the way, there are no more such properties in the HttpRequest for ASP.NET Core (headers only).

If you need to grab the client IP information, use any approach from the aforementioned (yours or suggested) links.

If you need to retrieve the familiar HttpContext instance (for example, outside the ApiController, for example, DelegatingHandler/aka middleware), use any of the approaches illustrated in this (already mentioned as well).

If you need to retrieve it inside the ApiController, it may be even easier (don't forget about required null ref checks and self-hosted WebAPI solutions):

public class YourApiController : ApiController {
    public HttpResponseMessage YourActionName() {
        var request = new HttpContextWrapper(CurrentContext).Request;
        ...
    }
}


public class YourAuditHandler : DelegatingHandler {
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
        string ipAddress = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "0.0.0.0";
        ...
    }
}
Community
  • 1
  • 1
Mikhail
  • 9,186
  • 4
  • 33
  • 49