隐藏

.net MVC记录访客信息

发布:2020/9/12 11:20:24作者:管理员 来源:本站 浏览次数:1021

使用sqlserver + EF + Linq 记录访客登录基本信息

首先建一个sql表,具体记哪些信息就建什么字段吧,我生成的model如下


public class InfoLog
    {
        #region Model
        private int _id;
        private string _uid;
        private string _vistip;
        private DateTime _visttime;
        private string _browser;
        private string _browserversion;
        /// <summary>
        /// 
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string UID
        {
            set { _uid = value; }
            get { return _uid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string VistIP
        {
            set { _vistip = value; }
            get { return _vistip; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime VistTime
        {
            set { _visttime = value; }
            get { return _visttime; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Browser
        {
            set { _browser = value; }
            get { return _browser; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string BrowserVersion
        {
            set { _browserversion = value; }
            get { return _browserversion; }
        }
        #endregion Model
    }
添加信息方法

private void VisitLog()
        {
            try
            {
                InfoLog logModel = new InfoLog();
                logModel.Browser = Request.Browser.Browser;
                logModel.BrowserVersion = Request.Browser.Version;
                logModel.UID = Request.Url.Query.ToUpper();
                logModel.VistIP = GetIp();
                logModel.VistTime = DateTime.Now;
                exec.SaveVistLog(logModel);
            }
            catch (Exception ex)
            {
                //GoToInfoPage("应用程序错误,信息:" + ex.ToString());
            }
        }

        private string GetIp()
        {
            //可以隔过代理IP获得真实IP
            string userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            //没有代理服务器,如果有代理服务器获取的是代理服务器的IP
            if (userIP == null || userIP == "")
            {
                userIP = Request.ServerVariables["REMOTE_ADDR"];
            }
            return userIP;
        }