Public Static classhttprequestmessageextensions {Private Const stringHttpContext ="Ms_httpcontext"; Private Const stringRemoteendpointmessage ="System.ServiceModel.Channels.RemoteEndpointMessageProperty"; Private Const stringOwincontext ="Ms_owincontext"; Public Static stringGetclientipaddress ( Thishttprequestmessage request) { //web-hosting. Needs reference to System.Web.dll if(Request. Properties.containskey (HttpContext)) {DynamicCTX =request. Properties[httpcontext]; if(CTX! =NULL) { returnCTX. request.userhostaddress; } } //self-hosting. Needs reference to System.ServiceModel.dll. if(Request. Properties.containskey (Remoteendpointmessage)) {DynamicRemoteendpoint =request. Properties[remoteendpointmessage]; if(Remoteendpoint! =NULL) { returnremoteendpoint.address; } } //self-hosting using Owin. Needs reference to Microsoft.Owin.dll. if(Request. Properties.containskey (Owincontext)) {DynamicOwincontext =request. Properties[owincontext]; if(Owincontext! =NULL) { returnowinContext.Request.RemoteIpAddress; } } return NULL; } }
WEBAPI Get link Client IP address