Esempio n. 1
0
        public LoginInfo GetLoginInfo(Guid token)
        {
            return(CacheHelper.Get <LoginInfo>(string.Format(_LoginInfoKeyFormat, token), () =>
            {
                using (var dbContext = new AccountDbContext())
                {
                    //如果有超时的,启动超时处理
                    var timeoutList = dbContext.FindAll <LoginInfo>(p => DbFunctions.DiffMinutes(DateTime.Now, p.LastAccessTime) > _UserLoginTimeoutMinutes);
                    //var timeoutList = dbContext.FindAll<LoginInfo>();
                    if (timeoutList.Count > 0)
                    {
                        foreach (var li in timeoutList)
                        {
                            dbContext.LoginInfos.Remove(li);
                        }
                    }

                    dbContext.SaveChanges();


                    var loginInfo = dbContext.FindAll <LoginInfo>(l => l.LoginToken == token).FirstOrDefault();
                    if (loginInfo != null)
                    {
                        loginInfo.LastAccessTime = DateTime.Now;
                        dbContext.Update <LoginInfo>(loginInfo);
                    }

                    return loginInfo;
                }
            }));
        }
Esempio n. 2
0
        public LoginInfo Login(string loginName, string password)
        {
            LoginInfo loginInfo = null;

            password  = Encrypt.MD5(password);
            loginName = loginName.Trim();

            using (var dbContext = new AccountDbContext())
            {
                var user = dbContext.Users.Include("Roles").Where(u => u.LoginName == loginName && u.Password == password && u.IsActive).FirstOrDefault();
                if (user != null)
                {
                    var ip = Fetch.UserIp;
                    loginInfo = dbContext.FindAll <LoginInfo>(p => p.LoginName == loginName && p.ClientIP == ip).FirstOrDefault();
                    if (loginInfo != null)
                    {
                        loginInfo.LastAccessTime = DateTime.Now;
                    }
                    else
                    {
                        loginInfo          = new LoginInfo(user.ID, user.LoginName);
                        loginInfo.ClientIP = ip;
                        loginInfo.Avatar   = user.ProfileImg;
                        loginInfo.BusinessPermissionList = user.BusinessPermissionList;
                        dbContext.Insert <LoginInfo>(loginInfo);
                    }
                }
            }

            return(loginInfo);
        }
Esempio n. 3
0
        public bool CheckVerifyCode(string verifyCodeText, Guid guid)
        {
            using (var dbContext = new AccountDbContext())
            {
                var verifyCode = dbContext.FindAll <VerifyCode>(v => v.Guid == guid && v.VerifyText == verifyCodeText).LastOrDefault();
                if (verifyCode != null)
                {
                    dbContext.VerifyCodes.Remove(verifyCode);
                    dbContext.SaveChanges();

                    //清除验证码大于2分钟还没请求的
                    var expiredTime = DateTime.Now.AddMinutes(-2);
                    foreach (var code in dbContext.VerifyCodes)
                    {
                        if (code.CreateTime < expiredTime)
                        {
                            dbContext.VerifyCodes.Remove(code);
                        }
                    }
                    //dbContext.VerifyCodes.Where(v => (v.CreateTime < expiredTime)).Delete();

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Esempio n. 4
0
        public void SaveUser(User user)
        {
            using (var dbContext = new AccountDbContext())
            {
                if (user.ID > 0)
                {
                    dbContext.Update <User>(user);

                    var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList();
                    user.Roles = roles;
                    dbContext.SaveChanges();
                }
                else
                {
                    var existUser = dbContext.FindAll <User>(u => u.LoginName == user.LoginName);
                    if (existUser.Count > 0)
                    {
                        throw new BusinessException("LoginName", "此登录名已存在!");
                    }
                    else
                    {
                        dbContext.Insert <User>(user);

                        var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList();
                        user.Roles = roles;
                        dbContext.SaveChanges();
                    }
                }
            }
        }
Esempio n. 5
0
 public IEnumerable <User> GetUserList(int accountType)
 {
     using (var dbContext = new AccountDbContext())
     {
         return(dbContext.FindAll <User>(u => u.AccountType == accountType));
     }
 }
Esempio n. 6
0
 public IEnumerable <User> GetUserLists(int accountType)
 {
     using (var dbContext = new AccountDbContext())
     {
         return(dbContext.FindAll <User>(u => u.AccountType == accountType || u.SecondTotally.Contains("9")));
     }
 }
Esempio n. 7
0
        public LoginInfo Login(string loginName, string password, string ip = null)
        {
            LoginInfo loginInfo = null;

            //password = Encrypt.MD5(password);
            loginName = loginName.Trim();

            using (var dbContext = new AccountDbContext())
            {
                var user = dbContext.Users.Include("Roles").Where(u => u.LoginName == loginName && u.Password == password && u.IsActive).FirstOrDefault();
                if (user != null)
                {
                    ip        = ip ?? Fetch.UserIp;
                    loginInfo = dbContext.FindAll <LoginInfo>(p => p.LoginName == loginName && p.ClientIP == ip).FirstOrDefault();
                    if (loginInfo != null)
                    {
                        loginInfo.LastAccessTime = DateTime.Now;
                    }
                    else
                    {
                        loginInfo = new LoginInfo(user.ID, user.LoginName);
                        if (user.Roles != null && user.Roles.Count > 0 && user.Roles.Exists(u => u.Name == "系统管理员"))   //判断是否系统管理员
                        {
                            loginInfo.EnumLoginAccountType = (int)EnumLoginAccountType.Administrator;
                        }
                        loginInfo.ClientIP = ip;
                        loginInfo.BusinessPermissionList = user.BusinessPermissionList;
                        dbContext.Insert <LoginInfo>(loginInfo);
                    }
                }
            }

            return(loginInfo);
        }
Esempio n. 8
0
        public void Logout(Guid token)
        {
            using (var dbContext = new AccountDbContext())
            {
                var loginInfo = dbContext.FindAll <LoginInfo>(l => l.LoginToken == token).FirstOrDefault();
                if (loginInfo != null)
                {
                    dbContext.Delete <LoginInfo>(loginInfo);
                }
            }

            CacheHelper.Remove(string.Format(_LoginInfoKeyFormat, token));
        }
Esempio n. 9
0
        public LoginInfo Login(string loginName, string password, int Workcell)
        {
            LoginInfo loginInfo = null;

            password  = Encrypt.MD5(password);
            loginName = loginName.Trim();

            using (var dbContext = new AccountDbContext())
            {
                var users = dbContext.Users.Include("Roles").Where(u => u.LoginName == loginName && u.Password == password && u.IsActive);
                if (users.Count() > 0)
                {
                    var ip   = Fetch.UserIp;
                    var user = users.Where(u => u.Workcell == Workcell).FirstOrDefault();
                    if (user == null)
                    {
                        LoginInfo res = new LoginInfo();
                        res.Workcell = 0;
                        return(res);
                    }
                    loginInfo = dbContext.FindAll <LoginInfo>(p => p.LoginName == loginName && p.ClientIP == ip).FirstOrDefault();
                    if (loginInfo != null)
                    {
                        loginInfo.LastAccessTime = DateTime.Now;
                        loginInfo.Workcell       = Workcell;
                    }
                    else
                    {
                        loginInfo          = new LoginInfo(user.ID, user.LoginName);
                        loginInfo.ClientIP = ip;
                        loginInfo.BusinessPermissionList = user.BusinessPermissionList;
                        loginInfo.Workcell = Workcell;
                        dbContext.Insert <LoginInfo>(loginInfo);
                    }
                }
                else
                {
                    return(null);
                }
            }

            return(loginInfo);
        }
Esempio n. 10
0
 public void SaveUser(User user)
 {
     using (var dbContext = new AccountDbContext())
     {
         if (user.ID > 0)
         {
             dbContext.Update <User>(user);
         }
         else
         {
             var existUser = dbContext.FindAll <User>(u => u.LoginName == user.LoginName);
             if (existUser.Count > 0)
             {
                 throw new BusinessException("LoginName", "此登录名已存在!");
             }
             else
             {
                 dbContext.Insert <User>(user);
             }
         }
     }
 }