Asp.net core 如何在ASP.NET核心中写入Request.ServerVariables[“HTTP”ux“uForwarded”ufor“”?

Asp.net core 如何在ASP.NET核心中写入Request.ServerVariables[“HTTP”ux“uForwarded”ufor“”?,asp.net-core,Asp.net Core,您好,我正在mvc应用程序中使用以下代码行,运行良好 Request.ServerVariables[“HTTP\u X\u FORWARDED\u FOR”] 如何在asp.net core中获取IP地址。我在asp.net内核中编写了以下代码,但它总是返回null 受保护的字符串GetClientIPAddress() { #如果调试 返回_configuration.GetSection(“IPAddress”)?.值; #否则 string ipAddress=string.Empty

您好,我正在mvc应用程序中使用以下代码行,运行良好

Request.ServerVariables[“HTTP\u X\u FORWARDED\u FOR”]

如何在asp.net core中获取IP地址。我在asp.net内核中编写了以下代码,但它总是返回null

受保护的字符串GetClientIPAddress()
{
#如果调试
返回_configuration.GetSection(“IPAddress”)?.值;
#否则
string ipAddress=string.Empty;
if(Request.HttpContext.Connection.RemoteIpAddress!=null)
{
ipAddress=Request.HttpContext.Connection.RemoteIpAddress.ToString();
}
返回IP地址;
#恩迪夫

}
请参阅并使用使用X-Forwarded-For标头值更新RemoteIpAddress的。

在asp.net core中使用请求.ServerVariables[“HTTP\U X\U Forwarded\U For”]时,您面临的问题是什么?您是否尝试使用
Request.Headers[“HTTP\u X\u FORWARDED\u FOR”]
?您好,Chetan,Request.Headers还返回空值