c# 记录客户端IP地址的方法

using System;  

using System.Net;  

using System.Text;  

using System.Threading;  

  

public class VisitsRecordsDAL  

{  

    public void Add(string visitRecord)  

    {  

        // 数据库操作逻辑  

        // ...  

    }  

}  

  

public class MyFilter : ActionFilterAttribute  

{  

    private VisitsRecordsDAL visitsRecordsDAL = new VisitsRecordsDAL();  

    private static readonly object lockObject = new object();  

  

    public override void OnActionExecuting(ActionExecutingContext filterContext)  

    {  

        string hostName = string.Empty;  

        string ip = string.Empty;  

        string ipv4 = string.Empty;  

        string ipv6 = string.Empty;  

        string finalIp = string.Empty;  

  

        try  

        {  

            if (!string.IsNullOrEmpty(filterContext.HttpContext.Request.ServerVariables["HTTP_VIA"]))  

            {  

                ip = Convert.ToString(filterContext.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);  

            }  

            if (string.IsNullOrEmpty(ip))  

            {  

                ip = filterContext.HttpContext.Request.UserHostAddress;  

            }  

  

            IPAddress ipAddress = null;  

            IPAddress.TryParse(ip, out ipAddress); // Try parsing IP address to check if it's IPv4 or IPv6.  

            if (ipAddress is IPv6Address ipv6Address)  

            {  

                ipv6 = ipv6Address.ToString();  

                finalIp = ipv6; // Use the IPv6 address for logging and database storage if it exists.  

            }  

            else if (ipAddress is IPv4Address ipv4Address)  

            {  

                ipv4 = ipv4Address.ToString();  

                finalIp = ipv4; // Use the IPv4 address for logging and database storage if it exists.  

            }  

            else if (!string.IsNullOrEmpty(ip)) // If it's neither IPv4 nor IPv6, use the original IP for logging and database storage.  

            {  

                finalIp = ip;  

            }  

            else // No IP address found, log an error or handle accordingly.  

            {  

                // Log an error or handle accordingly, e.g., continue without IP address or throw an exception.  

                return; // Return without further processing if no IP address is found.  

            }  

  

            // Log the visit record with IP address and hostname (if available).  

            hostName = Dns.GetHostEntry(finalIp).HostName; // Get the hostname if available for logging purposes.  

            string visitRecord = $"主机名: {hostName} IP: {finalIp}"; // Construct the visit record string for logging and database storage.  

            lock (lockObject) // Use a lock to ensure thread safety when accessing the database.  

            {  

                visitsRecordsDAL.Add(visitRecord); // Add the visit record to the database. Consider using a transaction or other mechanisms for data integrity if necessary.  

            }  

        }  

        catch (Exception ex) // Catch any exceptions that may occur during the execution of the filter.  

        {  

            // Log the exception or handle accordingly, e.g., continue without further processing or throw an exception with a detailed error message.  

            // You can use a logging framework like NLog or log4net to log exceptions in your application.  

        }  

    }  

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值