Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the dictionary key "MS_HttpContext" defined for HttpRequestMessage.Properties?

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)

like image 828
SpiritBob Avatar asked Aug 22 '19 10:08

SpiritBob


1 Answers

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";
        ...
    }
}
like image 176
Mikhail Avatar answered Sep 20 '22 16:09

Mikhail