Request.UserHostAddress return IP address of Load Balancer

前端 未结 3 1546
无人及你
无人及你 2021-02-06 03:13

I have a critical line of code in my site that worked in our development environment but not on production. Well, I say it worked in development but the truth is it gave

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 04:02

    Used this code to inspect production environment... It worked for me:

        System.Web.HttpRequest oRequest = System.Web.HttpContext.Current.Request;
    
        string header;
        string ip;
    
        header = "HTTP_X_FORWARDED_FOR";
        ip = oRequest.ServerVariables[header];
        Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine);
    
        header = "REMOTE_ADDR";
        ip = oRequest.ServerVariables[header];
        Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine);
    
        header = "HTTP_CLIENT_IP";
        ip = oRequest.ServerVariables[header];
        Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine);
    
        header = "Request.UserHostAddress";
        ip = oRequest.UserHostAddress;
        Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine);
    

提交回复
热议问题