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

Sup*_*Bob 6 c# asp.net asp.net-web-api

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();
}
Run Code Online (Sandbox Code Playgroud)

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)

Mik*_*ail 5

我想找到文档,该文档详细解释了MS_HttpContext的功能/拥有的内容。

在https://referencesource.microsoft.com/上似乎未描述System.Web.Http。*程序集。

但是,所有这些项目现在都以Open?的形式托管在GitHub上。来源https://github.com/aspnet/AspNetWebStack。到目前为止,我假设在将System.Web-> HttpContext分配给System.Web.Http-> Request的例程中使用了此常数/键。

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

另一个事件与CORS和测试有关。您可以克隆此存储库,并在寻找详细信息时深入搜索“ MS_HttpContext”的出现。但是,我不确定它的文档。

定义此密钥的位置,或类似的密钥,例如HTTP_X_FORWARDED_FOR,REMOTE_ADDR等。

所有这些字典键从何而来?

这些请求的属性(即服务器变量)是从客户端在此处发送的相应标头创建(几乎重复)的(适用于HttpContext-ASP.NET WebForms / MVC):

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

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

顺便说一下,ASP.NET Core的HttpRequest中没有更多此类属性(仅标题)。

如果您需要获取客户端IP信息,请使用上述(您或您建议的)链接中的任何方法。

如果需要检索熟悉的HttpContext实例(例如,在ApiController外部,例如DelegatingHandler / aka中间件),请使用此示例中已说明的方法(也已提到)。

如果您需要在ApiController内检索它,它可能甚至更容易(不要忘记必需的null ref检查和自托管的WebAPI解决方案):

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";
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)