zoukankan      html  css  js  c++  java
  • 获取登录的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. }  
  • 相关阅读:
    JavaScript中运算符的优先级
    JS中在当前日期上追加一天或者获取上一个月和下一个月
    Window命令行工具操作文件
    多线程Worker初尝试
    基于gulp的前端自动化开发构建新
    cURL和file_get_contents实现模拟post请求
    Thinkphp5使用validate实现验证功能
    微信小程序wx.pageScrollTo的替代方案
    js设计模式之代理模式以及订阅发布模式
    js设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/Vam8023/p/4500288.html
Copyright © 2011-2022 走看看