获取登录的IP或者信息

这是转载的,也不想去检查性能,对于这些成熟的代码,发在这里完全是懒,仅此而已!

1、获取客户端IP 

  1. /// <summary>  
  2. /// 获取客户端Ip  
  3. /// </summary>  
  4. /// <returns></returns>  
  5. public String GetClientIp()  
  6. {  
  7.     String clientIP = "";  
  8.     if (System.Web.HttpContext.Current != null)  
  9.     {  
  10.         clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  11.         if (string.IsNullOrEmpty(clientIP) || (clientIP.ToLower() == "unknown"))  
  12.         {  
  13.             clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];  
  14.             if (string.IsNullOrEmpty(clientIP))  
  15.             {  
  16.                 clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
  17.             }  
  18.         }  
  19.         else  
  20.         {  
  21.             clientIP = clientIP.Split(',')[0];  
  22.         }  
  23.     }  
  24.     return clientIP;  
  25. }  

2、服务器端获取客户端请求IP和客户端机器名称

 
  1. /// <summary>  
  2. /// 服务器端获取客户端请求IP和客户端机器名称  
  3. /// </summary>  
  4. public static void GetClientInfo()  
  5. {  
  6.     OperationContext context = OperationContext.Current;  
  7.     MessageProperties messageProperties = context.IncomingMessageProperties;  
  8.     RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;  
  9.     HttpRequestMessageProperty requestProperty = messageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;  
  10.     string clientIp = !string.IsNullOrEmpty(requestProperty.Headers["X-Real-IP"]) ? requestProperty.Headers["X-Real-IP"] : endpointProperty.Address;  
  11.     string clientName = Environment.MachineName;  
  12.     Console.WriteLine("ClientIp: " + clientIp + "clientName:" + clientName);  
  13. }  
原文地址:https://www.cnblogs.com/Vam8023/p/4500288.html