반응형
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
반응형
'sourcecode' 카테고리의 다른 글
null이 아닌 경우 javascript 검사 (0) | 2023.09.21 |
---|---|
GDB가 16진수 모드에서 모든 값을 출력하도록 하는 방법은? (0) | 2023.09.21 |
PowerShell에 env 변수가 없으면 어떻게 설정합니까? (0) | 2023.09.16 |
word press를 mariadb(도커)에 연결할 수 없습니다. (0) | 2023.09.16 |
Oracle Storeed Procedure에서 두 개의 반환 값을 가져오는 방법 (0) | 2023.09.16 |