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

发布时间:2023年12月22日
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.  

        }  

    }  

}

文章来源:https://blog.csdn.net/weixin_50784508/article/details/135151843
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。