Example #1
0
        public OperateStatus CheckLogin(BaseUser User)
        {
            OperateStatus op = new OperateStatus {
                IsSuccessful = false, Message = "初始异常!"
            };
            var aa = 999;
            var bb = "111111";
            var cc = DEncrypt.Get32_MD5Lower(bb, null);
            var dd = DEncrypt.Get32_MD5Lower(cc + aa, null);

            try
            {
                //取得静态数据--登录失效时间
                int LoginStateTime = int.Parse(GlobalStaticParam.GetByCode("LoginStateTime").ToString());

                #region 用户名密码验证
                //根据用户名查询数据
                var queryuser = activeContext.Org_User.FirstOrDefault(p => p.Id == User.Id || p.LoginName == User.LoginName ||
                                                                      (!string.IsNullOrEmpty(p.Phone) && p.Phone == User.Phone) ||
                                                                      (!string.IsNullOrEmpty(p.Email) && p.Email == User.Email));
                if (queryuser == null) //如果用户名不存在
                {
                    op.Message = "用户不存在,请检查用户名!";
                    return(op);
                }
                //用户被冻结
                if (queryuser.IsFreeze)
                {
                    op.Message = "登录失败!用户已被冻结!";
                    return(op);
                }

                //如果用户名存在
                //1.取得提交的密码明文
                //明文加密为初始密码(第一次加密)
                //因为前台已经加密,所以这里就用提交的密文作为第一次加密内容,不再不加密了.注意:前台加密要和后台加密算法相同
                string pwtext = User.PassWord; //前台提交的时候请使用此属性

                //取得用户随机数
                var userRandom = queryuser.Random;

                //用提交的密文+用户随机数,再次加密,生成提交的最后密码
                var lastPassWord = DEncrypt.Get32_MD5Lower(pwtext + userRandom, null);

                if (pwtext != queryuser.SourcePW || lastPassWord != queryuser.PassWord)
                {
                    op.Message = "密码不正确!";
                    return(op);
                }
                #endregion

                #region  查看是否有用户登录状态
                var LoginState = (from a in activeContext.Sys_UserLoginState
                                  where (a.LoginName.Equals(queryuser.LoginName))
                                  select a).FirstOrDefault();
                if (LoginState == null)
                {
                    Sys_UserLoginState UsersLoginState = new Sys_UserLoginState();
                    UsersLoginState.Id         = Guid.NewGuid();
                    UsersLoginState.SessionId  = Guid.NewGuid().ToString().Replace("-", "");
                    UsersLoginState.UserId     = queryuser.Id;
                    UsersLoginState.LoginName  = queryuser.LoginName;
                    UsersLoginState.LastTime   = DateTime.Now;
                    UsersLoginState.EqpMark    = User.EquipmentMark;
                    UsersLoginState.PeriodTime = UsersLoginState.LastTime.AddHours(LoginStateTime); //延长时间
                    activeContext.Sys_UserLoginState.Add(UsersLoginState);
                    activeContext.SaveChanges();
                }
                else
                {
                    LoginState.EqpMark    = User.EquipmentMark;
                    LoginState.LastTime   = DateTime.Now;
                    LoginState.PeriodTime = LoginState.LastTime.AddHours(LoginStateTime); //延长时间
                    activeContext.SaveChanges();
                }
                #endregion


                op.IsSuccessful = true;
                op.Message      = "";
                //op.Data = returnUser;
            }
            catch (Exception ex)
            {
                op.IsSuccessful = false;
                op.Message      = ex.Message;
            }
            return(op);
        }