获取调用action方法的服务器的IP地址

时间:2015-09-07 15:28:15

标签: c# .net asp.net-mvc asp.net-mvc-5

我在系统A中有以下操作方法,它使用WebClient在系统B上调用另一个操作方法: -

public async Task<ActionResult> Scan()
       {
           try
           {

               string currentURL = System.Web.Configuration.WebConfigurationManager.AppSettings["scanningURL"];
               using (WebClient wc = new WebClient())
               {
                   string url = currentURL + "home/scanserver?tokenfromtms=" + "*****" + "&FQDN=allscan" ;
                   var json = await  wc.DownloadStringTaskAsync(url);
                   TempData["messagePartial"] = string.Format("Scan has been completed. Scan reported generated");

               }




           }

和系统B内的动作方法,将使用上述动作方法调用: -

 public async Task<ActionResult> ScanServer(string tokenf, string FQDN) 
        {
            string Token = System.Web.Configuration.WebConfigurationManager.AppSettings["Token"];//get the token from the web.config, this should be encrypted

            if (tokenf != Token || Request.UserHostAddress != System.Web.Configuration.WebConfigurationManager.AppSettings["CallingIP"]) 

现在我要做的是,在系统B内检查正在提交的请求是否来自系统A的IP地址。那么在系统B上使用Request.UserHostAddress的动作方法会给我调用其动作方法的系统的IP地址(在我的情况下是系统A吗?)

1 个答案:

答案 0 :(得分:0)

您可以将IHttpModule用于创建请求过滤器。看一下这篇文章,这可能有所帮助:

http://www.codeproject.com/Articles/30907/The-Two-Interceptors-HttpModule-and-HttpHandlers

如果您在其他页面中需要这个,请尝试使其更通用。您可以在http上下文中使用它并重用它。顺便说一下,在标题上传递授权而不是查询字符串也会更好。