如何:从 ASP.NET MVC 的请求中获取客户端 IP 地址 [英] HOWTO: Get client IP address from request in ASP.NET MVC

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

问题描述

我在安装了 IIS 8.5 的 Web 服务器上部署了一个 ASP.NET MVC 应用程序(使用 .NET 4.5).

I have an ASP.NET MVC application (using .NET 4.5) deployed on a web server which has IIS 8.5 installed.

我创建了一个自定义控制器类,我在其中做一些事情,它继承自 System.Web.Mvc.Controller:

I have created a custom controller class where I do some stuff and it inherits from System.Web.Mvc.Controller:

public partial class MyCustomController : System.Web.Mvc.Controller
{
    // Here my stuff
}

然后,我的所有控制器(除了少数控制器)都继承自我的自定义控制器,例如:

Then, all my controllers (except a few ones), inherit from my custom controller, for example:

public partial class OneController : MyCustomController
{
   // Here some stuff
}

我的目标:

  1. 现在,我需要获取当前正在创建的客户端 IP 地址对我的 ASP.NET MVC 应用程序的请求.所以我想实施我的自定义控制器 MyCustomController 中的一个方法,即返回该客户端 IP.在这一点上这可能吗?如果是这样怎么办?
  2. 此外,我怎么知道传入的请求是否来自本地 IP 地址 (localhost) 127.0.0.1,如果是,则丢弃此请求,我的意思是,什么都不做?

推荐答案

您可以在 ASP.NET MVC 中使用 HttpRequest.ServerVariables 获取客户端的 IP 地址.REMOTE_ADDR 变量给出了客户端的 IP 地址.

You can use HttpRequest.ServerVariables to get the IP address of a client in ASP.NET MVC. The REMOTE_ADDR variable gives the IP address of the client.

您可以直接在控制器页面上使用以下方法,并从您的视图或任何需要的地方调用它

You can directly use the below method to your controller page and call it from your view or wherever you need it

   public string GetIp()  
   {  
      string ip = 
      System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
      if (string.IsNullOrEmpty(ip))  
      {  
        ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
      }  
    return IP;  
   }  

获取 IP 地址的第二种方法是使用 ASP.NET 的内置功能.这里我们使用 Page 类的 Request 属性,它为所请求的页面获取一个 HttpRequest 类的对象.HttpRequest 是一个密封类,它使 ASP.NET 能够在 Web 请求期间读取客户端浏览器发送的 HTTP 值.我们访问 HttpRequest 类的 UserHostAddress 属性来获取访问者的 IP 地址.

The second method of getting an IP address is using the built-in functionality of ASP.NET. Here we use the Request property of the Page class, which gets an object of HttpRequest class for the requested page. The HttpRequest is a sealed class that enables ASP.NET to read the HTTP values sent by the client browser during a Web request. We access the UserHostAddress property of the HttpRequest class to get the IP Address of the visitor.

    private void GetIpAddress(out string userip)  
    {  
      userip = Request.UserHostAddress;  
      if (Request.UserHostAddress != null)  
     {  
       Int64 macinfo = new Int64();  
       string macSrc = macinfo.ToString("X");  
       if (macSrc == "0")  
       {  
        if (userip == "127.0.0.1")  
        {  
            Response.Write("visited Localhost!");  
        }  
        else  
        {  
            lblIPAdd.Text = userip;  
         }     
     }  
  }  
}  

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

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