• 获取登录的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. }  
  • 相关阅读:
    Codeforces 1017E The Supersonic Rocket 凸包,计算几何,字符串,KMP
    Codeforces 1017F The Neutral Zone 数论
    51Nod1253 Kundu and Tree 容斥原理
    扩展中国剩余定理 (exCRT) 的证明与练习
    简单布局
    自创一个百变布局
    ajax上传文件
    DIV+CSS布局
    windowbuilder
    SWT开发工具
  • 原文地址:https://www.cnblogs.com/Vam8023/p/4500288.html
Copyright © 2020-2023  润新知