상세 컨텐츠

본문 제목

클라이언트 IP 추출

C#

by 탑~! 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);
            }

'C#' 카테고리의 다른 글

c# Windows 가상키보드 호출  (0) 2019.05.09
특수문자 포함여부  (0) 2019.03.06
Ping 테스트 방법  (0) 2018.09.06
자동 시작하는 프로그램  (0) 2018.09.06
SubClass 혹은 BaseClass 체크하기 ( System.Type )  (0) 2018.09.06

관련글 더보기