我怎样才能在ASP.Net MVC中的客户端的IP地址? [英] How can I get the client's IP address in ASP.Net MVC?

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

问题描述

我完全新的asp.net的MVC堆栈,我不知道发生了什么事,以简单的页面对象和请求对象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 what I want to do is to pull out the client PC's IP address, but I fail to understand how the current MVC structure has changed all of this.

据我可以理解,大多数变量对象都有被Htt的prequest变种更换?

有人关心共享一些资源?真正的东东海在ASP.Net MVC世界学习。 :)

Anybody care to share some resources? 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?

推荐答案

简单的答案是使用Htt$p$pquest.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;
            ..
        }
    }
}

,但如果该请求已经被一个或多个传递,代理服务器那么IP地址由<返回的href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.userhostaddress%28v=vs.110%29.aspx\">Htt$p$pquest.UserHostAddress属性将是中继的请求的最后一个代理服务器的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-转发,对于的HTTP标头。除了没有保证的请求具有的X转发-For头,也不能保证在X - 转发,对于一直没有的 欺骗

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

以上code提供了客户端的IP地址,而不诉诸仰视的集合。请求属性是控制器(或查看)中可用。因此强似Page类给你的函数可以传递Request对象获得相同的结果:

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天全站免登陆