| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 | using System;using System.Collections.Generic;using System.Linq;using System.Net.Http;using System.ServiceModel.Channels;using System.Web;using System.Web.Http;namespace iBemsDataService.Controllers{    public class ClientAccessController : ApiController    {        [ActionName("ClientIpAddress")]        public ClientInfo GetClientIpAddress()        {            ClientInfo clientInfo = new ClientInfo();            clientInfo.ip = GetIPAddress();            return clientInfo;        }        protected string GetIPAddress()        {            System.Web.HttpContext context = System.Web.HttpContext.Current;            string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];            if (!string.IsNullOrEmpty(ipAddress))            {                string[] addresses = ipAddress.Split(',');                if (addresses.Length != 0)                {                    return addresses[0];                }            }            return context.Request.ServerVariables["REMOTE_ADDR"];        }        private string GetClientIp(HttpRequestMessage request = null)        {            request = request ?? Request;            if (request.Properties.ContainsKey("MS_HttpContext"))            {                return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;            }            else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))            {                RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];                return prop.Address;            }            else if (HttpContext.Current != null)            {                return HttpContext.Current.Request.UserHostAddress;            }            else            {                return null;            }        }        public class ClientInfo        {            public String ip { get; set; }        }    }}
 |