String clientIP =
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)?
HttpContext.Current.Request.UserHostAddress:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Run Code Online (Sandbox Code Playgroud)
HttpContext.Current.Request.UserHostAddress
Run Code Online (Sandbox Code Playgroud)
这不会尝试考虑代理.为此,你可以使用Request.ServerVariables["HTTP_X_FORWARDED_FOR"].但是,请确保你不会盲目相信,因为它可能是伪造的.最好保留您信任的IP白名单.