如何在ASP.NET MVC中获取客户端的IP地址?

如何在ASP.NET MVC中获取客户端的IP地址?I’mtotallynewtotheASP.NETMVCstack,andIwaswonderingwhathappenedtothesimplePageobje

大家好,又见面了,我是你们的朋友全栈君。

本文翻译自:How can I get the client’s IP address in ASP.NET MVC?

I’m totally new to the ASP.NET MVC stack, and I was wondering what happened to the simple Page object and the Request ServerVariables object? 我是ASP.NET MVC堆栈的新手,我想知道简单的Page对象和Request ServerVariables对象发生了什么?

Basically, I want to to pull out the client PC’s IP address, but I fail to understand how the current MVC structure has changed all of this. 基本上,我想提取客户端PC的IP地址,但我无法理解当前的MVC结构如何改变了所有这些。

As far as I can understand, most of the variable objects has been replaced by the HttpRequest variants . 据我所知, 大多数变量对象已被HttpRequest变体所取代

Anybody care to share some resources? 有人关心分享一些资源吗? There is really a sea of stuff to learn in the ASP.NET MVC world. ASP.NET MVC世界中有很多东西要学习。 :) :)

For example, I have a static class with this current function. 例如,我有一个带有当前函数的静态类。 How do I get the same result using ASP.NET MVC? 如何使用ASP.NET MVC获得相同的结果?

public static int getCountry(Page page)
{
    return getCountryFromIP(getIPAddress(page));
}

public static string getIPAddress(Page page)
{
    string szRemoteAddr = page.Request.ServerVariables["REMOTE_ADDR"];
    string szXForwardedFor = page.Request.ServerVariables["X_FORWARDED_FOR"];
    string szIP = "";

    if (szXForwardedFor == null)
    {
        szIP = szRemoteAddr;
    }
    else
    {
        szIP = szXForwardedFor;

        if (szIP.IndexOf(",") > 0)
        {
            string [] arIPs = szIP.Split(',');

            foreach (string item in arIPs)
            {
                if (!isPrivateIP(item))
                {
                    return item;
                }
            }
        }
    }
    return szIP;
}

And how do I call this function from the controller page? 如何从控制器页面调用此功能?


#1楼

参考:https://stackoom.com/question/AoWW/如何在ASP-NET-MVC中获取客户端的IP地址


#2楼

A lot of the code here was very helpful, but I cleaned it up for my purposes and added some tests. 这里的很多代码都非常有用,但我为了我的目的清理了它并添加了一些测试。 Here’s what I ended up with: 这是我最终得到的:

using System;
using System.Linq;
using System.Net;
using System.Web;

public class RequestHelpers
{
    public static string GetClientIpAddress(HttpRequestBase request)
    {
        try
        {
            var userHostAddress = request.UserHostAddress;

            // Attempt to parse.  If it fails, we catch below and return "0.0.0.0"
            // Could use TryParse instead, but I wanted to catch all exceptions
            IPAddress.Parse(userHostAddress);

            var xForwardedFor = request.ServerVariables["X_FORWARDED_FOR"];

            if (string.IsNullOrEmpty(xForwardedFor))
                return userHostAddress;

            // Get a list of public ip addresses in the X_FORWARDED_FOR variable
            var publicForwardingIps = xForwardedFor.Split(',').Where(ip => !IsPrivateIpAddress(ip)).ToList();

            // If we found any, return the last one, otherwise return the user host address
            return publicForwardingIps.Any() ? publicForwardingIps.Last() : userHostAddress;
        }
        catch (Exception)
        {
            // Always return all zeroes for any failure (my calling code expects it)
            return "0.0.0.0";
        }
    }

    private static bool IsPrivateIpAddress(string ipAddress)
    {
        // http://en.wikipedia.org/wiki/Private_network
        // Private IP Addresses are: 
        //  24-bit block: 10.0.0.0 through 10.255.255.255
        //  20-bit block: 172.16.0.0 through 172.31.255.255
        //  16-bit block: 192.168.0.0 through 192.168.255.255
        //  Link-local addresses: 169.254.0.0 through 169.254.255.255 (http://en.wikipedia.org/wiki/Link-local_address)

        var ip = IPAddress.Parse(ipAddress);
        var octets = ip.GetAddressBytes();

        var is24BitBlock = octets[0] == 10;
        if (is24BitBlock) return true; // Return to prevent further processing

        var is20BitBlock = octets[0] == 172 && octets[1] >= 16 && octets[1] <= 31;
        if (is20BitBlock) return true; // Return to prevent further processing

        var is16BitBlock = octets[0] == 192 && octets[1] == 168;
        if (is16BitBlock) return true; // Return to prevent further processing

        var isLinkLocalAddress = octets[0] == 169 && octets[1] == 254;
        return isLinkLocalAddress;
    }
}

And here are some NUnit tests against that code (I’m using Rhino Mocks to mock the HttpRequestBase, which is the M<HttpRequestBase> call below): 以下是针对该代码的一些NUnit测试(我使用Rhino Mocks来模拟HttpRequestBase,这是下面的M <HttpRequestBase>调用):

using System.Web;
using NUnit.Framework;
using Rhino.Mocks;
using Should;

[TestFixture]
public class HelpersTests : TestBase
{
    HttpRequestBase _httpRequest;

    private const string XForwardedFor = "X_FORWARDED_FOR";
    private const string MalformedIpAddress = "MALFORMED";
    private const string DefaultIpAddress = "0.0.0.0";
    private const string GoogleIpAddress = "74.125.224.224";
    private const string MicrosoftIpAddress = "65.55.58.201";
    private const string Private24Bit = "10.0.0.0";
    private const string Private20Bit = "172.16.0.0";
    private const string Private16Bit = "192.168.0.0";
    private const string PrivateLinkLocal = "169.254.0.0";

    [SetUp]
    public void Setup()
    {
        _httpRequest = M<HttpRequestBase>();
    }

    [TearDown]
    public void Teardown()
    {
        _httpRequest = null;
    }

    [Test]
    public void PublicIpAndNullXForwardedFor_Returns_CorrectIp()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(null);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(GoogleIpAddress);
    }

    [Test]
    public void PublicIpAndEmptyXForwardedFor_Returns_CorrectIp()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(string.Empty);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(GoogleIpAddress);
    }

    [Test]
    public void MalformedUserHostAddress_Returns_DefaultIpAddress()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(MalformedIpAddress);
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(null);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(DefaultIpAddress);
    }

    [Test]
    public void MalformedXForwardedFor_Returns_DefaultIpAddress()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(MalformedIpAddress);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(DefaultIpAddress);
    }

    [Test]
    public void SingleValidPublicXForwardedFor_Returns_XForwardedFor()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(MicrosoftIpAddress);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(MicrosoftIpAddress);
    }

    [Test]
    public void MultipleValidPublicXForwardedFor_Returns_LastXForwardedFor()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(GoogleIpAddress + "," + MicrosoftIpAddress);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(MicrosoftIpAddress);
    }

    [Test]
    public void SinglePrivateXForwardedFor_Returns_UserHostAddress()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(Private24Bit);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(GoogleIpAddress);
    }

    [Test]
    public void MultiplePrivateXForwardedFor_Returns_UserHostAddress()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        const string privateIpList = Private24Bit + "," + Private20Bit + "," + Private16Bit + "," + PrivateLinkLocal;
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(privateIpList);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(GoogleIpAddress);
    }

    [Test]
    public void MultiplePublicXForwardedForWithPrivateLast_Returns_LastPublic()
    {
        // Arrange
        _httpRequest.Stub(x => x.UserHostAddress).Return(GoogleIpAddress);
        const string privateIpList = Private24Bit + "," + Private20Bit + "," + MicrosoftIpAddress + "," + PrivateLinkLocal;
        _httpRequest.Stub(x => x.ServerVariables[XForwardedFor]).Return(privateIpList);

        // Act
        var ip = RequestHelpers.GetClientIpAddress(_httpRequest);

        // Assert
        ip.ShouldEqual(MicrosoftIpAddress);
    }
}

#3楼

Request.ServerVariables["REMOTE_ADDR"] should work – either directly in a view or in the controller action method body (Request is a property of Controller class in MVC, not Page). Request.ServerVariables["REMOTE_ADDR"]应该可以工作 – 直接在视图中或在控制器动作方法体中(Request是MVC中Controller类的属性,而不是Page)。

It is working.. but you have to publish on a real IIS not the virtual one. 它工作..但你必须在真正的IIS上发布而不是虚拟的。


#4楼

How I account for my site being behind an Amazon AWS Elastic Load Balancer (ELB): 我如何说明我的网站背后是亚马逊AWS Elastic Load Balancer(ELB):

public class GetPublicIp {

    /// <summary>
    /// account for possbility of ELB sheilding the public IP address
    /// </summary>
    /// <returns></returns>
    public static string Execute() {
        try {
            Console.WriteLine(string.Join("|", new List<object> {
                    HttpContext.Current.Request.UserHostAddress,
                    HttpContext.Current.Request.Headers["X-Forwarded-For"],
                    HttpContext.Current.Request.Headers["REMOTE_ADDR"]
                })
            );

            var ip = HttpContext.Current.Request.UserHostAddress;
            if (HttpContext.Current.Request.Headers["X-Forwarded-For"] != null) {
                ip = HttpContext.Current.Request.Headers["X-Forwarded-For"];
                Console.WriteLine(ip + "|X-Forwarded-For");
            }
            else if (HttpContext.Current.Request.Headers["REMOTE_ADDR"] != null) {
                ip = HttpContext.Current.Request.Headers["REMOTE_ADDR"];
                Console.WriteLine(ip + "|REMOTE_ADDR");
            }
            return ip;
        }
        catch (Exception ex) {
            Console.Error.WriteLine(ex.Message);
        }
        return null;
    }
}

#5楼

The simple answer is to use the HttpRequest.UserHostAddress property . 简单的答案是使用HttpRequest.UserHostAddress属性

Example: From within a Controller: 示例:在Controller中:

using System;
using System.Web.Mvc;

namespace Mvc.Controllers
{
    public class HomeController : ClientController
    {
        public ActionResult Index()
        {
            string ip = Request.UserHostAddress;

            ...
        }
    }
}

Example: From within a helper class: 示例:从辅助类中:

using System.Web;

namespace Mvc.Helpers
{
    public static class HelperClass
    {
        public static string GetIPHelper()
        {
            string ip = HttpContext.Current.Request.UserHostAddress;
            ..
        }
    }
}

BUT, if the request has been passed on by one, or more, proxy servers then the IP address returned by HttpRequest.UserHostAddress property will be the IP address of the last proxy server that relayed the request. 但是,如果请求已由一个或多个代理服务器传递,则HttpRequest.UserHostAddress属性返回的IP地址将是中继请求的最后一个代理服务器的IP地址。

Proxy servers MAY use the de facto standard of placing the client’s IP address in the X-Forwarded-For HTTP header. 代理服务器可以使用将客户端的IP地址放在X-Forwarded-For HTTP标头中的事实标准。 Aside from there is no guarantee that a request has a X-Forwarded-For header, there is also no guarantee that the X-Forwarded-For hasn’t been SPOOFED . 除了无法保证请求具有X-Forwarded-For标头之外,还无法保证X-Forwarded-For未被SPOOFED


Original Answer 原始答案

Request.UserHostAddress

The above code provides the Client’s IP address without resorting to looking up a collection. 上面的代码提供了客户端的IP地址,而无需查找集合。 The Request property is available within Controllers (or Views). Request属性在Controllers(或Views)中可用。 Therefore instead of passing a Page class to your function you can pass a Request object to get the same result: 因此,您可以传递一个Request对象来获取相同的结果,而不是将Page类传递给您的函数:

public static string getIPAddress(HttpRequestBase request)
{
    string szRemoteAddr = request.UserHostAddress;
    string szXForwardedFor = request.ServerVariables["X_FORWARDED_FOR"];
    string szIP = "";

    if (szXForwardedFor == null)
    {
        szIP = szRemoteAddr;
    }
    else
    {
        szIP = szXForwardedFor;
        if (szIP.IndexOf(",") > 0)
        {
            string [] arIPs = szIP.Split(',');

            foreach (string item in arIPs)
            {
                if (!isPrivateIP(item))
                {
                    return item;
                }
            }
        }
    }
    return szIP;
}

#6楼

In a class you might call it like this: 在课堂上你可以这样称呼它:

public static string GetIPAddress(HttpRequestBase request) 
{
    string ip;
    try
    {
        ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (!string.IsNullOrEmpty(ip))
        {
            if (ip.IndexOf(",") > 0)
            {
                string[] ipRange = ip.Split(',');
                int le = ipRange.Length - 1;
                ip = ipRange[le];
            }
        } else
        {
            ip = request.UserHostAddress;
        }
    } catch { ip = null; }

    return ip; 
}

I used this in a razor app with great results. 我在剃须刀应用程序中使用了这个,效果很好。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162906.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Spring框架介绍及使用

    Spring框架介绍及使用Spring框架—控制反转(IOC)1Spring框架概述1.1什么是Spring1.2Spring的优点1.3Spring的体系结构2入门案例:(IoC)2.1导入jar包2.2目标类2.3配置文件2.4测试3入门案例:DI3.1目标类3.2dao3.3service3.4配置文件3.5测试4依赖注入…

    2022年6月18日
    20
  • Idea激活码最新教程2023.2.2版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2023.2.2版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2023 2 2 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2023 2 2 成功激活

    2025年5月26日
    3
  • 日本の行政区画–都道府県

    日本の行政区画–都道府県中国の行政区画–省市自治区に類似して、日本の行政区画は都道府県(とどうふけん)である。全部で一都、一道、二府と43県がある。一都(いっと)とは東京都で、日本の政治、経済、文化などの中心である。一道(いちどう)は北海道で、開発が他の土地より遅い。二府(にふ)は、京都府と大阪府で、関西の主な部分で、歴史と経済の面で非常に重要な地区である。日本の県は中国の省に当たり(面積はずっと狭いが)、全部で43県

    2022年7月11日
    29
  • mac xmind快捷键

    mac xmind快捷键tab:新建分支command+z:撤销command+”+”:放大command+”-“:缩小shift+enter:文字换行转载于:https://www.cnblogs.com/yintingting/p/5678890.html

    2022年5月3日
    150
  • 数字常用格式_数字字体大全对照表

    数字常用格式_数字字体大全对照表三位一逗:使用“N”(使用n也可以,不区分大小写),“N”后面的数字是小数位数//三位一逗,保留5位小数Console.WriteLine($"{9999.12345.ToString(

    2022年8月2日
    5
  • java map 二维数组_Java二维数组实现简单Map

    java map 二维数组_Java二维数组实现简单Map这些天频繁的在使用二维数组,让我觉得二维数组要比Map更灵活多变,以前和别人提起“数据结构”总能听到有人问:“如果编程语言里没有HashMap,你能自己实现一个Map来用么?”。熟练了二维数组,今天我就来尝试实现一个最简单的Map吧,我没有参考网上的例子,也没去想数据结构书中是怎么讲的,纯粹的自己个一个设计方案,中途遇到很多问题,但还是逐个解决了,还有很多不足之处,希望大家能帮我指点指点,一起交流…

    2022年5月24日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号