【问题标题】:ASP.NET web application visitor counterASP.NET Web 应用程序访问者计数器
【发布时间】:2016-09-07 05:31:31
【问题描述】:

我有一个简单的 Web 应用程序,我需要用它来跟踪访问者。每个访问者的信息都存储在下表中:

访客详情:

| ID | ComputerName | VisitDate | CountryCode | Platform | Browser | BrowserVersion |

[ComputerName] 是访问者的 IP 地址。

该表包含 ipv6 记录,这会导致计算访问者数量时出现问题,或者有时会导致 Token 错误。

强制访问者使用 ipv4 是否明智? 如果是,怎么做?

【问题讨论】:

    标签: asp.net web-applications ipv6 ipv4


    【解决方案1】:
        public string IpAddress()
    {
    string strIpAddress;
    strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (strIpAddress == null)
    {
    strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
    }
    return strIpAddress;
    }
    
    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
     { 
    if (IPA.AddressFamily.ToString() == "InterNetwork")
     { 
    IP4Address = IPA.ToString(); 
    break;
     } 
    }
    

    请参考以下链接:

    http://www.4guysfromrolla.com/articles/071807-1.aspx http://tutorialgenius.blogspot.in/2010/09/aspnet-get-ipv4-address-even-if-user-is.html

    【讨论】:

    • 谢谢,这正是我想要的。
    【解决方案2】:
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["NoOfVisitors"] = 0;
    }
    
    
    
    
    
    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Application.Lock();
        Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
        Application.UnLock();
    }
    
    in .aspx page
    
    <asp:Label runat="server" ID="lbluser" />
    in .aspx.cs
    
    protected void Page_Load(object sender, EventArgs e)
    {
        lbluser.Text = Application["NoOfVisitors"].ToString();
    }
    

    【讨论】:

    • 不错,但主要问题是 ipv6
    【解决方案3】:
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
    
            WebpageCounter.SaveVisitor(new WebpageVisitor()
            {
                VisitorIP = HttpContext.Current.Request.UserHostAddress,
                VisitedOn = DateTime.Now
            });
        }
    -------------
    Below Code is for IP Address
    string ipaddress;
    ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (ipaddress == "" || ipaddress == null)
        ipaddress = Request.ServerVariables["REMOTE_ADDR"];
    

    【讨论】:

    • HTTP_X_FORWARDED_FOR 可能返回多个值。我如何确保我只会获得 ipv4
    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 2013-02-24
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    相关资源
    最近更新 更多