如何在 ASP.NET MVC 中获取客户端的 IP 地址? [英] How can I get the client's IP address in ASP.NET MVC?

查看:109
本文介绍了如何在 ASP.NET MVC 中获取客户端的 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ASP.NET MVC 堆栈完全陌生,我想知道简单的 Page 对象和 Request ServerVariables 对象发生了什么变化?

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?

基本上,我想提取客户端 PC 的 IP 地址,但我无法理解当前的 MVC 结构如何改变了这一切.

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.

据我所知,大多数的变量对象已被 HttpRequest 变体替换.

有人愿意分享一些资源吗?在 ASP.NET MVC 世界中真的有很多东西要学习.:)

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?

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?

推荐答案

简单的答案是使用 HttpRequest.UserHostAddress 属性.

示例:在控制器中:

using System;
using System.Web.Mvc;

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

            ...
        }
    }
}

示例:来自助手类:

using System.Web;

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

但是,如果请求已由一个或多个代理服务器 然后是 HttpRequest.UserHostAddress 返回的 IP 地址属性 将是最后一个中继请求的代理服务器的 IP 地址.

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.

代理服务器可以使用事实上的标准,将客户端的 IP 地址放在X-Forwarded-For HTTP 标头.除了不能保证请求有 X-Forwarded-For 标头之外,也不能保证 X-Forwarded-For 没有 欺骗.

Proxy servers MAY use the de facto standard of placing the client's IP address in the X-Forwarded-For HTTP header. 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.

原答案

Request.UserHostAddress

上面的代码提供了客户端的 IP 地址,而无需求助于查找集合.Request 属性在控制器(或视图)中可用.因此,您可以传递一个 Request 对象来获得相同的结果,而不是将 Page 类传递给您的函数:

The above code provides the Client's IP address without resorting to looking up a collection. The Request property is available within Controllers (or Views). Therefore instead of passing a Page class to your function you can pass a Request object to get the same result:

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;
}

这篇关于如何在 ASP.NET MVC 中获取客户端的 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆