小编use*_*537的帖子

X-Forwarded-For:如何在 ASP.Net MVC 中获取客户端的 IP 地址?

我对 asp.net mvc 堆栈完全陌生,我想知道 Request.Header 对象发生了什么?

基本上我想做的是拉出设备(PC)IP地址,但我无法检索所需的输出。我也尝试过 Request.ServerVariables 对象,但结果始终保持为 NULL。

我正在使用 asp.net MVC。此功能是否需要任何更改:

public static string GetIP()
    {
        string functionReturnValue = null;
        //Gets IP of actual device versus the proxy (WAP Gateway)
        functionReturnValue = HttpContext.Current.Request.Headers["X-FORWARDED-FOR"]; //functionReturnValue = null
        if (string.IsNullOrEmpty(functionReturnValue))
        {
            functionReturnValue = HttpContext.Current.Request.Headers["X-Forwarded-For"];//functionReturnValue = null
            if (string.IsNullOrEmpty(functionReturnValue))
            {
                //If not using a proxy then get the device IP
                // GetIP = Context.Request.ServerVariables("REMOTE_ADDR")
                if (string.IsNullOrEmpty(functionReturnValue))
                {
                    //If not using a proxy then get the device IP
                    functionReturnValue = HttpContext.Current.Request.Headers["X-CLIENT-IP"];//functionReturnValue = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc

0
推荐指数
1
解决办法
7664
查看次数

标签 统计

asp.net ×1

asp.net-mvc ×1

c# ×1