本文介绍了如何获取服务器IP地址,即ASP.NET中服务器的IPv4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过以下但是在托管应用程序后它返回:: 1作为IP地址。但是,当用户请求网站时,我想知道用户是否请求该特定IP。所以我想检索服务器IPV4地址。



我尝试过:



  private  字符串 GetIPAddress()
{
IPServer = string .Empty;
IPServer = Convert.ToString(HttpContext.Current.Request.ServerVariables [ HTTP_X_FORWARDED_FOR] )!= null ? Convert.ToString(HttpContext.Current.Request.ServerVariables [ HTTP_X_FORWARDED_FOR]): .Empty;

if string .IsNullOrEmpty(IPServer))
{
IPServer = Request.ServerVariables [ REMOTE_ADDR];
}
return IPServer;
}
解决方案

I've tried as below but it's returning ::1 as the IP Address after Hosting the Application. But here when the user requests for the website i want to know whether the user requested to that particular IP or not. So I wanna retrieve the server IPV4 address.

What I have tried:

private String GetIPAddress()
{
IPServer = string.Empty;
         IPServer=Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) != null ? Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) : string.Empty;

if (string.IsNullOrEmpty(IPServer))
{
IPServer = Request.ServerVariables["REMOTE_ADDR"];
}
return IPServer;
}
解决方案


这篇关于如何获取服务器IP地址,即ASP.NET中服务器的IPv4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:41
查看更多