Esempio n. 1
0
        public Task Invoke(HttpContext context)
        {
            string token = "";

            if (context.Request.Cookies.TryGetValue("MRCTOKEN", out token))
            {
                string userinfo  = EncryptHelper.DesDecrypt(token, KeyTool.GetEncryptKey());
                string orginInfo = RedisHelper.Get(userinfo);
                if (orginInfo.IsNullOrEmpty())
                {
                    context.Items["islogin"] = false;
                    return(this._next(context));
                }
                ;
                AdminSession userSession = JsonHelper.Deserialize <AdminSession>(orginInfo);
                if (context.GetClientIP() != userSession.LoginIP)
                {
                    context.Items["islogin"] = false;
                }
                else
                {
                    context.Items["user"]    = userSession;
                    context.Items["islogin"] = true;
                }
            }
            return(this._next(context));
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="oldPassword">明文</param>
        /// <param name="newPassword">明文</param>
        public void ChangePassword(string userID, string oldPassword, string newPassword)
        {
            //EncryptHelper.DesEncrypt(newPassword);



            Sys_UserLogOn userLogOn = this.DbContext.Query <Sys_UserLogOn>().Where(a => a.UserId == userID).First();

            string encryptedOldPassword = EncryptHelper.DesEncrypt(oldPassword, userLogOn.UserSecretkey);

            if (encryptedOldPassword != userLogOn.UserPassword)
            {
                throw new InvalidInputException("旧密码不正确");
            }

            string newUserSecretkey     = KeyTool.GetEncryptKey();
            string newEncryptedPassword = EncryptHelper.DesEncrypt(newPassword, newUserSecretkey);

            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Update <Sys_UserLogOn>(a => a.UserId == userID, a => new Sys_UserLogOn()
                {
                    UserSecretkey = newUserSecretkey, UserPassword = newEncryptedPassword
                });
            });
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="password">前端传过来的是经过md5加密后的密码</param>
        /// <param name="user"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CheckLogin(string loginName, string password, out Sys_User user, out string msg)
        {
            user = null;
            msg  = null;

            loginName.NotNullOrEmpty();
            password.NotNullOrEmpty();

            var view = this.DbContext.JoinQuery <Sys_User, Sys_UserLogOn>((u, userLogOn) => new object[]
            {
                JoinType.InnerJoin, u.Id == userLogOn.UserId
            })
                       .Select((u, userLogOn) => new { User = u, UserLogOn = userLogOn });

            loginName = loginName.ToLower();
            if (CommonTool.IsMobilePhone(loginName))
            {
                view = view.Where(a => a.User.MobilePhone == loginName);
            }
            else if (CommonTool.IsEmail(loginName))
            {
                view = view.Where(a => a.User.Email == loginName);
            }
            else
            {
                view = view.Where(a => a.User.AccountName == loginName);
            }

            view = view.Where(a => a.User.State != AccountState.Closed);

            var viewEntity = view.FirstOrDefault();

            if (viewEntity == null)
            {
                msg = "账户不存在,请重新输入";
                return(false);
            }
            if (!viewEntity.User.IsAdmin())
            {
                if (viewEntity.User.State == AccountState.Disabled)
                {
                    msg = "账户被禁用,请联系管理员";
                    return(false);
                }
            }

            Sys_User      userEntity      = viewEntity.User;
            Sys_UserLogOn userLogOnEntity = viewEntity.UserLogOn;

            string dbPassword = EncryptHelper.DesEncrypt(password, KeyTool.GetEncryptKey()).ToMD5();

            if (dbPassword != userLogOnEntity.UserPassword)
            {
                msg = "密码不正确,请重新输入";
                return(false);
            }

            DateTime lastVisitTime = DateTime.Now;

            this.DbContext.Update <Sys_UserLogOn>(a => a.Id == userLogOnEntity.Id, a => new Sys_UserLogOn()
            {
                LogOnCount = a.LogOnCount + 1, PreviousVisitTime = userLogOnEntity.LastVisitTime, LastVisitTime = lastVisitTime
            });
            user = userEntity;
            return(true);
        }
Esempio n. 4
0
        public void Add(AddUserInput input)
        {
            this.Trim(input);

            input.Validate();

            if (input.AccountName.IsNullOrEmpty() && input.MobilePhone.IsNullOrEmpty() && input.Email.IsNullOrEmpty())
            {
                throw new InvalidInputException("用户名/手机号码/邮箱至少填一个");
            }

            string accountName = null;

            if (input.AccountName.IsNotNullOrEmpty())
            {
                accountName = input.AccountName.ToLower();
                CommonTool.EnsureAccountNameLegal(accountName);
                bool exists = this.DbContext.Query <Sys_User>().Where(a => a.AccountName == accountName).Any();
                if (exists)
                {
                    throw new InvalidInputException("用户名[{0}]已存在".ToFormat(input.AccountName));
                }
            }

            string mobilePhone = null;

            if (input.MobilePhone.IsNotNullOrEmpty())
            {
                mobilePhone = input.MobilePhone;
                if (CommonTool.IsMobilePhone(mobilePhone) == false)
                {
                    throw new InvalidInputException("请输入正确的手机号码");
                }

                bool exists = this.DbContext.Query <Sys_User>().Where(a => a.MobilePhone == mobilePhone).Any();
                if (exists)
                {
                    throw new InvalidInputException("手机号码[{0}]已存在".ToFormat(mobilePhone));
                }
            }

            string email = null;

            if (input.Email.IsNotNullOrEmpty())
            {
                email = input.Email.ToLower();
                if (CommonTool.IsEmail(email) == false)
                {
                    throw new InvalidInputException("请输入正确的邮箱地址");
                }

                bool exists = this.DbContext.Query <Sys_User>().Where(a => a.Email == email).Any();
                if (exists)
                {
                    throw new InvalidInputException("邮箱地址[{0}]已存在".ToFormat(input.Email));
                }
            }

            Sys_User user = new Sys_User();

            user.AccountName = accountName;
            user.Name        = input.Name;
            user.Gender      = input.Gender;
            user.MobilePhone = mobilePhone;
            user.Birthday    = input.Birthday;
            user.WeChat      = input.WeChat;
            user.Email       = email;
            user.Description = input.Description;
            user.State       = AccountState.Normal;

            string userSecretkey     = KeyTool.GetEncryptKey();
            string encryptedPassword = EncryptHelper.DesEncrypt(input.Password, userSecretkey);

            Sys_UserLogOn logOnEntity = new Sys_UserLogOn();

            logOnEntity.Id            = IdHelper.CreateStringSnowflakeId();
            logOnEntity.UserId        = user.Id;
            logOnEntity.UserSecretkey = userSecretkey;
            logOnEntity.UserPassword  = encryptedPassword;

            List <string>       roleIds   = input.GetRoles();
            List <Sys_UserRole> userRoles = roleIds.Select(a =>
            {
                return(new Sys_UserRole()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = user.Id,
                    RoleId = a,
                });
            }).ToList();

            user.RoleIds = string.Join(",", roleIds);

            List <string>      orgIds   = input.GetOrgs();
            List <Sys_UserOrg> userOrgs = orgIds.Select(a =>
            {
                return(new Sys_UserOrg()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = user.Id,
                    OrgId = a,
                    DisablePermission = false
                });
            }).ToList();

            user.OrgIds = string.Join(",", orgIds);

            List <string>       postIds   = input.GetPosts();
            List <Sys_UserPost> userPosts = postIds.Select(a =>
            {
                return(new Sys_UserPost()
                {
                    Id = IdHelper.CreateStringSnowflakeId(),
                    UserId = user.Id,
                    PostId = a
                });
            }).ToList();

            user.PostIds = string.Join(",", postIds);

            this.DbContext.DoWithTransaction(() =>
            {
                this.DbContext.Insert(user);
                this.DbContext.Insert(logOnEntity);
                this.DbContext.InsertRange(userRoles);
                this.DbContext.InsertRange(userOrgs);
                this.DbContext.InsertRange(userPosts);
            });
        }