sourcecode

WebMethod에서 발신자의 IP 주소를 얻으려면 어떻게 해야 합니까?

copyscript 2023. 9. 21. 21:10
반응형

WebMethod에서 발신자의 IP 주소를 얻으려면 어떻게 해야 합니까?

WebMethod에서 발신자의 IP 주소를 얻으려면 어떻게 해야 합니까?

[WebMethod]
public void Foo()
{
    // HttpRequest... ? - Not giving me any options through intellisense...
}

C# 및 ASP.NET 사용

HttpContext.현재.요청합니다.UserHostAddress가 당신이 원하는 것입니다.

주의 사항입니다.IP 주소는 클라이언트를 고유하게 식별하는 데 사용될 수 없습니다.NAT 방화벽과 기업 프록시는 어디에나 존재하며 단일 IP 뒤에 많은 사용자를 숨깁니다.

시도:

Context.Request.UserHostAddress

시도해 보기:

string ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

webMethod에서 시도해 본 적은 없지만 표준 HttpRequests에서 사용합니다.

HttpContext는 실제로 내부에서 사용할 수 있습니다.WebService기본 클래스를 사용합니다.Context.Request(또는HttpContext.Current현재 상황을 가리키기도 함)에 의해 제공된 회원에게 접근하기 위해HttpRequest.

저는 다음과 같은 기능을 만들었습니다.

static public string sGetIP()
{
    try
    {
        string functionReturnValue = null;

        String oRequestHttp =
            WebOperationContext.Current.IncomingRequest.Headers["User-Host-Address"];
        if (string.IsNullOrEmpty(oRequestHttp))
        {
            OperationContext context = OperationContext.Current;
            MessageProperties prop = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint =
                prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            oRequestHttp = endpoint.Address;
        }
        return functionReturnValue;
    }
    catch (Exception ex)
        {
            return "unknown IP";
        }
}

인트라넷에서만 작동하며 프록시 또는 네이팅이 있는 경우 원래 IP가 http 패킷의 다른 곳으로 이동되었는지 연구해야 합니다.

언급URL : https://stackoverflow.com/questions/130328/how-do-i-get-the-callers-ip-address-in-a-webmethod

반응형