Get the IP address of the client connecting to a C# .NET WebAPI application

后端 未结 1 1818
小鲜肉
小鲜肉 2021-01-05 20:03

I tried:

private const string HttpContext = \"MS_HttpContext\";
private const string RemoteEndpointMessage = \"System.ServiceModel.Channels.RemoteEndpointMe         


        
相关标签:
1条回答
  • 2021-01-05 20:19

    Here is an expanded version of what you have that works for me.

    static class HttpRequestMessageExtensions {
    
        private const string HttpContext = "MS_HttpContext";
        private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
        private const string OwinContext = "MS_OwinContext";
    
        public static string GetClientIpString(this HttpRequestMessage request) {
            //Web-hosting
            if (request.Properties.ContainsKey(HttpContext)) {
                dynamic ctx = request.Properties[HttpContext];
                if (ctx != null) {
                    return ctx.Request.UserHostAddress;
                }
            }
            //Self-hosting
            if (request.Properties.ContainsKey(RemoteEndpointMessage)) {
                dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
                if (remoteEndpoint != null) {
                    return remoteEndpoint.Address;
                }
            }
            //Owin-hosting
            if (request.Properties.ContainsKey(OwinContext)) {
                dynamic ctx = request.Properties[OwinContext];
                if (ctx != null) {
                    return ctx.Request.RemoteIpAddress;
                }
            }
            if (System.Web.HttpContext.Current != null) {
                return System.Web.HttpContext.Current.Request.UserHostAddress;
            }
            // Always return all zeroes for any failure
            return "0.0.0.0";
        }
    
        public static IPAddress GetClientIpAddress(this HttpRequestMessage request) {
            var ipString = request.GetClientIpString();
            IPAddress ipAddress = new IPAddress(0);
            if (IPAddress.TryParse(ipString, out ipAddress)) {
                return ipAddress;
            }
    
            return ipAddress;
        }
    
    }
    

    Assuming you are in a controller the above extension method allows for calls like:

    HttpRequestMessage request = this.Request;
    
    var ip = request.GetClientIpString();
    
    0 讨论(0)
提交回复
热议问题