///// <summary>
        ///// 获取IP地址
        ///// </summary>
        ///// <returns></returns>

        public static string GetIPAddress(HttpRequestBase request)
        {
            string ipv4 = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = IPA.ToString();
                    break;
                }
            }
            if (ipv4 != String.Empty)
            {
                return(ipv4);
            }
            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = IPA.ToString();
                    break;
                }
            }
            return(ipv4);
        }
Beispiel #2
0
    // This function gets the IP Address
    public static string GetIP4Address()
    {
        string IP4Address = string.Empty;

        foreach (IPAddress IPA in
                 Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }
        if (IP4Address != string.Empty)
        {
            return(IP4Address);
        }
        foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }
        return(IP4Address);
    }
Beispiel #3
0
        public static string GetIP4Address()
        {
            string IP4Address = String.Empty;

            //foreach (System.Net.IPAddress IPA in System.Net.Dns.GetHostAddresses(System.Web.HttpContext.Current.Request.UserHostAddress))
            //{
            //    if (IPA.AddressFamily.ToString() == "InterNetwork")
            //    {
            //        IP4Address = IPA.ToString();
            //        break;
            //    }
            //}

            //if (IP4Address != String.Empty)
            //{
            //    return IP4Address;
            //}

            foreach (System.Net.IPAddress IPA in System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return(IP4Address);
        }
Beispiel #4
0
        private static string GetIP4Address(string userHostAddress)
        {
            var IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(userHostAddress))
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                return(IP4Address);
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return(IP4Address);
        }
Beispiel #5
0
        // reference: http://www.4guysfromrolla.com/articles/071807-1.aspx
        private static string GetIP4Address(string userHostAddress)
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(userHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                return(IP4Address);
            }

            var addList = Dns.GetHostEntry(userHostAddress).AddressList;

            foreach (IPAddress ipa in addList)
            {
                if (ipa.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = ipa.ToString();
                    break;
                }
            }
            return(IP4Address);
        }
        private string GetIP4Address()
        {
            if (Request.UserHostAddress == "::1")
            {
                return("127.0.0.1");
            }
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                return(IP4Address);
            }
            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return(IP4Address);
        }
        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <returns></returns>

        public static string GetIPAddress(HttpContext httpContext)
        {
            string ipv4 = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(httpContext.Connection.RemoteIpAddress.ToString()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = IPA.ToString();
                    break;
                }
            }
            if (ipv4 != String.Empty)
            {
                return(ipv4);
            }
            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = IPA.ToString();
                    break;
                }
            }
            return(ipv4);
        }
        public void Encontrar_IP()
        {
            try
            {
                /*
                 * Este loop irá procurar por endereços DNS e determinar o IP
                 * da máquina utilizada
                 */
                foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (IPA.AddressFamily == AddressFamily.InterNetwork)
                    {
                        IP4_Endereco = IPA.ToString();
                        break;
                    }
                }
                textBox1.Text = IP4_Endereco;

                /*
                 * Será utilizado como porta padrão a porta 5001
                 */
                textBox2.Text = "5001";
            }
            catch { }
        }
Beispiel #9
0
        public static string GetIp(HttpRequest Request)
        {
            string ip4address = String.Empty;

            try
            {
                foreach (IPAddress IPA in Dns.GetHostAddresses(Request.UserHostAddress))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        ip4address = IPA.ToString();
                        break;
                    }
                }

                if (ip4address != String.Empty)
                {
                    return(ip4address);
                }

                foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        ip4address = IPA.ToString();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(ip4address);
        }
Beispiel #10
0
        public static string GetIP4Address()
        {
            string IP4Address = string.Empty;

            if (HttpContext.Current != null)
            {
                foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        IP4Address = IPA.ToString();
                        break; // TODO: might not be correct. Was : Exit For
                    }
                }
            }

            if (!string.IsNullOrEmpty(IP4Address))
            {
                return(IP4Address);
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break; // TODO: might not be correct. Was : Exit For
                }
            }

            return(IP4Address);
        }
Beispiel #11
0
        public static string GetIP4Address()
        {
            try
            {
                if (string.IsNullOrEmpty(globalVariables.IpAddress))
                {
                    string IP4Address = String.Empty;

                    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
                    {
                        if (IPA.AddressFamily == AddressFamily.InterNetwork)
                        {
                            IP4Address = IPA.ToString();
                            break;
                        }
                    }
                    globalVariables.IpAddress = IP4Address;
                }


                return(globalVariables.IpAddress);
            }
            catch
            { return("NO-IP"); }
        }
Beispiel #12
0
 /// <summary>
 /// /获取客户端的IPv4
 /// </summary>
 /// <returns></returns>
 public static string GetClientIPv4()
 {
     string ipv4 = String.Empty;
     foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
     {
         if (IPA.AddressFamily.ToString() == "InterNetwork")
         {
             ipv4 = IPA.ToString();
             break;
         }
     }
     if (ipv4 != String.Empty)
     {
         return ipv4;
     }
     foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
     {
         if (IPA.AddressFamily.ToString() == "InterNetwork")
         {
             ipv4 = IPA.ToString();
             break;
         }
     }
     return ipv4;
 }
Beispiel #13
0
        private string getip()
        {
            string _ip = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    _ip = IPA.ToString();
                    break;
                }
            }
            if (_ip != String.Empty)
            {
                return(_ip);
            }
            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    _ip = IPA.ToString();
                    break;
                }
            }
            return(_ip);
        }
Beispiel #14
0
        private string GetIP4Address()
        {
            string IP4Address = ""; // Context.Request.ServerVariables["REMOTE_ADDR"];

            //string qq = Dns.GetHostAddresses(Dns.GetHostName()).Where(p => p.AddressFamily.ToString() == "InterNetwork").Select(p => p.ToString()).FirstOrDefault();

            foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostName))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (string.IsNullOrEmpty(IP4Address))
            {
                IP4Address = Request.UserHostAddress;
            }

            if (IP4Address == "::1")
            {
                foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        IP4Address = IPA.ToString();
                        break;
                    }
                }
            }

            return(IP4Address);
        }
Beispiel #15
0
        public string GetIP4Address()
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(Request.ServerVariables["REMOTE_ADDR"].ToString()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                return(IP4Address);
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                try
                {
                    WriteLine("IP: " + IPA.ToString() + "; Family: " + IPA.AddressFamily.ToString() + "<br>");
                }
                catch { }
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    //break;
                }
            }

            return(IP4Address);
        }
        public static bool CanConnectToInternet() //Uses NSCI (http://technet.microsoft.com/en-us/library/cc766017.aspx)
        {
            string DnsCheck = "dns.msftncsi.com";

            try {
                IPAddress[] AddressList = Dns.GetHostAddresses(DnsCheck); //DIS THING
                bool        DNSFine     = false;
                foreach (IPAddress IPA in AddressList)
                {
                    if (IPA.ToString() == "131.107.255.255")
                    {
                        DNSFine = true;
                    }
                }
                if (DNSFine == false)
                {
                    return(false);
                }
            }
            catch { return(false); }

            string ConnectionCheck = "http://www.msftncsi.com/ncsi.txt";
            string Result          = GrabWebpage(ConnectionCheck);

            if (Result != "Microsoft NCSI")
            {
                return(false);
            }

            return(true);
        }
Beispiel #17
0
        public string getvisitorip()
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(System.Web.HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                return(IP4Address);
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return(IP4Address);
        }
        /// <summary>
        /// Gets the i p4 address.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public static string GetIP4Address(this HttpContextBase context)
        {
            string result = string.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(context.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    result = IPA.ToString();
                    break;
                }
            }

            if (result != string.Empty)
            {
                return(result);
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    result = IPA.ToString();
                    break;
                }
            }

            return(result);
        }
Beispiel #19
0
        /// <summary>
        /// Gets the address.
        /// </summary>
        /// <param name="Host">The host.</param>
        /// <param name="AddressFormat">The address format.</param>
        /// <returns>Returns IP address</returns>
        /// <remarks><seealso cref="AddressType"></seealso></remarks>
        public static string GetAddress(string Host, AddressType AddressFormat)
        {
            AddressFamily addrFamily = AddressFamily.InterNetwork;

            switch (AddressFormat)
            {
            case AddressType.IPv4:
                addrFamily = AddressFamily.InterNetwork;
                break;

            case AddressType.IPv6:
                addrFamily = AddressFamily.InterNetworkV6;
                break;
            }
            IPHostEntry IPE = Dns.GetHostEntry(Host);

            if (Host != IPE.HostName)
            {
                IPE = Dns.GetHostEntry(IPE.HostName);
            }
            foreach (IPAddress IPA in IPE.AddressList)
            {
                if (IPA.AddressFamily == addrFamily)
                {
                    return(IPA.ToString());
                }
            }
            return(string.Empty);
        }
        //public static String GetIP()
        //{
        //    String ip =
        //        HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        //    if (string.IsNullOrEmpty(ip))
        //    {
        //        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        //    }

        //    return ip;
        //}

        public static string GetIP4Address()
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                return(IP4Address);
            }

            //foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            //{
            //    if (IPA.AddressFamily.ToString() == "InterNetwork")
            //    {
            //        IP4Address = IPA.ToString();
            //        break;
            //    }
            //}

            //string IPAddress = HttpContext.Current.Request.UserHostAddress;
            //string IPAddress2 = HttpContext.Current.Request.Params["HTTP_CLIENT_IP"] ?? HttpContext.Current.Request.UserHostAddress;


            return(IP4Address);
        }
Beispiel #21
0
        public static string GetIP4Address(string s)
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(s))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            if (IP4Address != String.Empty)
            {
                return(IP4Address);
            }

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }

            return(IP4Address);
        }
Beispiel #22
0
        public static string IPCliente(TipoIP ipTipo)
        {
            try
            {
                //return HttpContext.Current.Request.UserHostName;
                if (ipTipo == TipoIP.V4)
                {
                    string IP4Address = String.Empty;

                    foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
                    {
                        if (IPA.AddressFamily.ToString() == "InterNetwork")
                        {
                            IP4Address = IPA.ToString();
                            break;
                        }
                    }

                    if (IP4Address != String.Empty)
                    {
                        return(IP4Address);
                    }

                    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
                    {
                        if (IPA.AddressFamily.ToString() == "InterNetwork")
                        {
                            IP4Address = IPA.ToString();
                            break;
                        }
                    }

                    return(IP4Address);
                }
                if (ipTipo == TipoIP.V6)
                {
                    System.Web.HttpContext context = System.Web.HttpContext.Current;
                    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                    if (!string.IsNullOrEmpty(ipAddress))
                    {
                        string[] addresses = ipAddress.Split(',');
                        if (addresses.Length != 0)
                        {
                            return(addresses[0]);
                        }
                    }

                    return(context.Request.ServerVariables["REMOTE_ADDR"]);
                }
                return("");
            }
            catch
            {
                return("");
            }
        }
Beispiel #23
0
        //get the ipv4 address only
        public void GetIP4AddressList()
        {
            label9.Text = "";

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    label9.Text += IPA.ToString() + "\r\n";
                }
            }
        }
Beispiel #24
0
        //get the ipv4 address only
        public void GetIP4AddressList()
        {
            listboxIP.Items.Clear();

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    listboxIP.Items.Add(IPA.ToString());
                }
            }
        }
Beispiel #25
0
            private static void getIp1()
            {
                var ips = Dns.GetHostAddresses(Dns.GetHostName());

                foreach (IPAddress IPA in ips)
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        strIP4Address = IPA.ToString();
                    }
                }
            }
Beispiel #26
0
        static public string GetIP()
        {
            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    return(IPA.ToString());
                }
            }

            return("No ip address found");
        }
Beispiel #27
0
        private void lblIP_Click(object sender, EventArgs e)
        {
            lblIP.Text = "";

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    string data = IPA.ToString();
                    lblIP.Text += IPA.ToString() + "\r\n";
                }
            }
        }
Beispiel #28
0
        public String ip()
        {
            string IP4Address = String.Empty;

            foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (IPA.AddressFamily == AddressFamily.InterNetwork)
                {
                    IP4Address = IPA.ToString();
                    break;
                }
            }
            return(IP4Address);
        }
        // 사용자 컴퓨터의 ip Address를 가져옴
        public static String getClientIPAddress()
        {
            String sRet = "";

            foreach (IPAddress IPA in Dns.GetHostAddresses(System.Net.Dns.GetHostName()))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    sRet = IPA.ToString();
                    break;
                }
            }
            return(sRet);
        }
        public static string GetClientIPv4()
        {
            string ipv4 = "127.0.0.1";

            foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
            {
                if (IPA.AddressFamily.ToString() == "InterNetwork")
                {
                    ipv4 = IPA.ToString();
                    break;
                }
            }
            return(ipv4);
        }