Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访客IP地址不显示_C#_Asp.net_Asp.net Mvc_Ip Address - Fatal编程技术网

C# 访客IP地址不显示

C# 访客IP地址不显示,c#,asp.net,asp.net-mvc,ip-address,C#,Asp.net,Asp.net Mvc,Ip Address,我在获取访问者ip地址时遇到问题 我使用的是asp.net mvc 我使用以下代码来保存访问者的IP地址,但不幸的是,它在我的本地网络上不起作用。许多人从局域网访问了我的页面,但没有捕获到任何一个,我有一个包含许多页面的网站,如果用户访问了另一个页面,它也不会捕获任何问题,因为我在主页索引中有代码 请让我知道我的代码有什么问题,以及我必须将代码放在项目中的什么位置,这样,如果用户单击链接而不是主页,它将捕获任何页面的用户和访问者 这是我的密码 家庭控制器 public ActionResult

我在获取访问者ip地址时遇到问题 我使用的是asp.net mvc 我使用以下代码来保存访问者的IP地址,但不幸的是,它在我的本地网络上不起作用。许多人从局域网访问了我的页面,但没有捕获到任何一个,我有一个包含许多页面的网站,如果用户访问了另一个页面,它也不会捕获任何问题,因为我在主页索引中有代码 请让我知道我的代码有什么问题,以及我必须将代码放在项目中的什么位置,这样,如果用户单击链接而不是主页,它将捕获任何页面的用户和访问者 这是我的密码 家庭控制器

public ActionResult Index()
        {
            string currip = string.Empty;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                currip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else if (System.Web.HttpContext.Current.Request.UserHostAddress.Length != 0)
            {
                currip = System.Web.HttpContext.Current.Request.UserHostAddress;

            }
         //   string currip = HttpContext.Request.UserHostAddress.ToString(); // tried this didnt work it gave me single ip for all visitors 
             //   vs.Ip = currip;
                vs.Ip = HttpContext.Request.UserHostAddress.ToString();
                vs.lastaccess = DateTime.Now;
                vs.visits += 1;
                _db.SaveChanges();           
           
           return view();
}
更新 使用时 currip=System.Web.HttpContext.Current.Request.UserHostAddress; 对于post方法,添加登录信息ip会出现,但对于visit,它不会出现,因为它是get方法 跟那有关吗

public static string getIPAddress(HttpRequestBase request)
    {
        string ipAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = request.ServerVariables["REMOTE_ADDR"];
        }

        return ipAddress;
    }
更新

您可以使用“访问”属性

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class VisitAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var descriptor = filterContext.ActionDescriptor;
        var controller = descriptor.ControllerDescriptor.ControllerName;
        var action = descriptor.ActionName;
        var user = filterContext.HttpContext.User.Identity.Name;

        // your code here 
     }
}
然后,您可以通过以下方式调用控制器或操作:

[Visit]
public class YourController : Controller
{

}

另外,我没有找到答案,虽然我试过了,但没有成功,而且答案在逻辑上是正确的,但代码的问题是 旧代码

我必须编辑我的代码 到


这可能是因为防火墙/DMZ/NAT。这是否回答了您的问题?我对此一无所知,我们正处于测试阶段,所以我不确定我们是否托管了它,它是否能在当前代码下工作,我已经尝试过了,如果用户登录,那么它在这个currip=System.Web.HttpContext.current.Request.UserHostAddress下工作正常;但不是visiting@Progman不,它没有记住,您可能需要调整防火墙/转发器/负载平衡器配置,以传递这些标题中的任何一个谢谢您@wowoot,如果我想在应用程序从任何页面(而不是主页)启动时调用此函数,将其放置在何处,以及我应该在调用函数中传递什么参数?您可以将其添加为中间件,以提取一次,然后将其作为会话变量传递到每个页面。你的答案看起来逻辑正确,但我不知道如何实现它。你能告诉我如何将所有这些结合在一起,你的代码在这里吗?你的意思是我应该将答案的第一部分放在那里请澄清是的,你将放置“getIPAddress”以及用于插入数据库的代码。“VisitAttribute”类您可以将其放入任何类中或为其创建一个新类。非常感谢,请让我知道要在函数字符串curip=getIPAddress中传递的参数(这里写什么);
vs.visits += 1;
_db.SaveChanges();   
vs.visits += 1;
_db.visit.add(vs); // here was the missing code 
_db.SaveChanges();