runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

http://stackoverflow.com/questions/3937773/wcf-security-using-client-ip-address

 

(exposed on WebHttpBinding). 

 

/// <summary>
        /// WCF 获取客户端IP,必须是WebServiceHost
        /// </summary>
        /// <returns></returns>
        public static string GetClientIpWcf()
        {
            OperationContext context = OperationContext.Current;
            MessageProperties messageProperties = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            HttpRequestMessageProperty requestProperty = messageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            string myip = !string.IsNullOrEmpty(requestProperty.Headers["X-Real-IP"]) ? requestProperty.Headers["X-Real-IP"] : endpointProperty.Address;

            return myip;
        }

 

posted on 2015-02-02 16:04  runliuv  阅读(279)  评论(0编辑  收藏  举报