C#

클라이언트 IP 추출

탑~! 2019. 1. 15. 09:21

using System.Net;
using System.Net.Sockets;

/// <summary>
        /// 사용자IP 조회 
        /// </summary>
        /// <returns></returns>
        public string GetRemoteIP()
        {
            // IPv6 형태를 IPv4 형태로
            string ip = string.Empty;

            try
            {
                foreach (IPAddress i in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
                {
                    if (i.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ip = i.ToString();
                        break;
                    }
                }

                if (ip == string.Empty)
                {
                    foreach (IPAddress i in Dns.GetHostAddresses(Dns.GetHostName()))
                    {
                        if (i.AddressFamily == AddressFamily.InterNetwork)
                        {
                            ip = i.ToString();
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string msg = ex.InnerException.ToString();
                //logger.Error("사용자 IP 조회 오류", ex);
            }

            return ip;
        }
        #endregion

 

 

 

bool isAuthOK = false;
            string ipAddr = GetRemoteIP();

            if (ipAddr.Contains("10.177") || ipAddr.Contains("10.176") || ipAddr.Contains("10.178") || ipAddr.Contains("10.20"))
            {
                isAuthOK = true;
            }
           

            if (!isAuthOK)
            {
                // Response.Redirect(Request.UrlReferrer.ToString());
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "navigate", string.Format("window.parent.history.back(-1);"), true);
            }