public void when_ip_is_valid() { Assert.Equal("0.0.0.12", IPConverter.ToIPAddress("12").ToString()); Assert.Equal("1.0.0.2", IPConverter.ToIPAddress("1.2").ToString()); Assert.Equal("1.2.3.4", IPConverter.ToIPAddress("1.2.3.4").ToString()); Assert.Equal("abcd:ef01:2345:6789:abcd:ef01:2345:6789", IPConverter.ToIPAddress("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789").ToString()); }
/// <summary> /// 获取当前请求的IP /// </summary> public IPAddress GetClientIP(HttpRequestBase httpRequest) { if (httpRequest == null) { return(null); } var ip = GetIpFromServerVariables(httpRequest.ServerVariables); if (string.IsNullOrWhiteSpace(ip)) { ip = httpRequest.UserHostAddress; } return(IPConverter.ToIPAddress(ip)); }
public void when_ip_is_invalid_should_return_null(string ip) { Assert.Null(IPConverter.ToIPAddress(ip)); }