public void ExtractIpUsingXForwardedForHeaderMultipleIp() { var options = SecureNativeConfigurationBuilder.DefaultConfigBuilder().Build(); var headers = new WebHeaderCollection { { "x-forwarded-for", "141.246.115.116, 203.0.113.1, 12.34.56.3" } }; var uri = new Uri("http://www.securenative.com/login"); var request = WebRequest.Create(uri); request.Headers = headers; var clientIp = RequestUtils.GetClientIpFromRequest((HttpWebRequest)request, options); Assert.AreEqual("141.246.115.116", clientIp); }
public void ExtractIpUsingXForwardedForHeaderIpv6() { var options = SecureNativeConfigurationBuilder.DefaultConfigBuilder().Build(); var headers = new WebHeaderCollection { { "x-forwarded-for", "f71f:5bf9:25ff:1883:a8c4:eeff:7b80:aa2d" } }; var uri = new Uri("http://www.securenative.com/login"); var request = WebRequest.Create(uri); request.Headers = headers; var clientIp = RequestUtils.GetClientIpFromRequest((HttpWebRequest)request, options); Assert.AreEqual("f71f:5bf9:25ff:1883:a8c4:eeff:7b80:aa2d", clientIp); }
public void ExtractRequestWithProxyHeadersMultipleIps() { var options = SecureNativeConfigurationBuilder.DefaultConfigBuilder() .WithProxyHeaders(new[] { "CF-Connecting-IP" }).Build(); var headers = new WebHeaderCollection { { "CF-Connecting-IP", "141.246.115.116, 203.0.113.1, 12.34.56.3" } }; var uri = new Uri("http://www.securenative.com/login"); var request = WebRequest.Create(uri); request.Headers = headers; var clientIp = RequestUtils.GetClientIpFromRequest((HttpWebRequest)request, options); Assert.AreEqual("141.246.115.116", clientIp); }
public void ExtractIpUsingPriorityWithoutXForwardedFor() { var options = SecureNativeConfigurationBuilder.DefaultConfigBuilder().Build(); var headers = new WebHeaderCollection { { "x-real-ip", "98.51.100.101" }, { "x-client-ip", "203.0.113.1, 141.246.115.116, 12.34.56.3" } }; var uri = new Uri("http://www.securenative.com/login"); var request = WebRequest.Create(uri); request.Headers = headers; var clientIp = RequestUtils.GetClientIpFromRequest((HttpWebRequest)request, options); Assert.AreEqual("203.0.113.1", clientIp); }
public static SecureNativeContextBuilder FromHttpRequest(HttpWebRequest request, SecureNativeOptions options) { var headers = RequestUtils.GetHeadersFromRequest(request, options); var clientToken = RequestUtils.GetCookieValueFromRequest(request, RequestUtils.SecurenativeCookie); if (string.IsNullOrEmpty(clientToken)) { clientToken = RequestUtils.GetSecureHeaderFromRequest(headers); } return(new SecureNativeContextBuilder() .WithUrl(request.RequestUri.ToString()) .WithMethod(request.Method) .WithHeaders(headers) .WithClientToken(clientToken) .WithIp(RequestUtils.GetClientIpFromRequest(request, options)) .WithRemoteIp(RequestUtils.GetRemoteIpFromRequest(request)) .WithBody(null)); }