Example #1
0
        public sys_user Login(string user, string pass)
        {
            sys_user u = null;

            using (var db = new SysContext())
            {
                u = db.sys_user.Find(user);
                if (u != null)
                {
                    if (!u.Password.Equals(pass))
                    {
                        // 密码错误
                        u = null;
                    }
                    //else if (u.IsEnable.HasValue && !u.IsEnable.Value)
                    else if (!u.IsEnable)
                    {
                        // 帐户未启用
                        u = null;
                    }
                }

                if (u != null)
                {
                    u.LoginCount++;
                    u.LastLoginDate = DateTime.Now;
                    db.SaveChanges();
                }
            }
            return(u);
        }
Example #2
0
        public void OnLogined(sys_user user)
        {
            // 记录登录日志
            using (var db = new SysContext())
            {
                var    hostIp     = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                string clientName = null;
                try
                {
                    System.Net.IPAddress   address = System.Net.IPAddress.Parse(hostIp);
                    System.Net.IPHostEntry ipInfor = System.Net.Dns.GetHostEntry(address);
                    clientName = ipInfor.HostName;
                }
                catch { }
                db.sys_loginHistory.Add(new sys_loginHistory
                {
                    UserCode  = user.UserCode,
                    UserName  = user.UserName,
                    HostIP    = hostIp,
                    HostName  = clientName,
                    LoginDate = DateTime.Now
                });
                db.SaveChanges();
            }

            // 执行 Session 初始化
            Dictionary <string, OneConnection> openConnections = new Dictionary <string, OneConnection>();

            foreach (var s in SysSessions)
            {
                try
                {
                    // 准备连接
                    var           connName = string.IsNullOrEmpty(s.Connection) ? DefaultConnection.Name : s.Connection;
                    OneConnection conn     = null;
                    if (openConnections.ContainsKey(connName))
                    {
                        conn = openConnections[connName];
                    }
                    else
                    {
                        conn = openConnections[connName] = CreateOneConnection(connName);
                    }

                    if (!string.IsNullOrEmpty(s.Condition))
                    {
                        // 进行条件测试
                        object val = conn.ExecuteScalar(FruitExpression.Replace(s.Condition));
                        if (val is bool && (bool)val == false)
                        {
                            continue;
                        }
                    }

                    if (!string.IsNullOrEmpty(s.T_SQL))
                    {
                        // 运行查询
                        object val = conn.ExecuteScalar(FruitExpression.Replace(s.T_SQL));
                        if (val != DBNull.Value && val != null)
                        {
                            HttpContext.Current.Session[s.SessionName] = val.ToString();
                            continue;
                        }
                    }

                    if (!string.IsNullOrEmpty(s.DefaultValue))
                    {
                        // 默认值
                        HttpContext.Current.Session[s.SessionName] = FruitExpression.Replace(s.DefaultValue);
                    }
                }
                catch (Exception e) {
                    System.Diagnostics.Debug.WriteLine(e);
                }
            }
            // 关闭所有打开过的连接
            foreach (var conn in openConnections.Values)
            {
                conn.Dispose();
            }
        }