Beispiel #1
0
        public ActionResult Index(FormCollection fc)
        {
            if (!string.IsNullOrEmpty(Request["name"]) && !string.IsNullOrEmpty(Request["pwd"]))
            {
                var name = Request["name"];
                var pwd  = Request["pwd"];

                var user = _userBll.Query(name);
                if (user == null)
                {
                    SetTempData("用户不存在,请核对账号是否正确!");
                    return(RedirectToAction("Index"));
                }
                if (user.IsLock)
                {
                    SetTempData("用户已锁定,请联系管理员进行解锁!");
                    return(RedirectToAction("Index"));
                }
                if (DesEncrypt.Encrypt(pwd, user.Salt) != user.Password)
                {
                    SetTempData("密码错误,请重新输入!");
                    return(RedirectToAction("Index"));
                }
                var roleIdList = user?.Roles.Select(x => x.RoleId).Distinct();

                //登陆重定向
                Session.Add("User", user);
                Session.Add("RoleIdList", roleIdList);

                var userIdentity = new UserIdentity(user.LoginName, true, 7200);
                foreach (var role in user.Roles)
                {
                    userIdentity.Roles.Add(role.Code);
                }

                //认证
                var token = AuthManager.Login(userIdentity);

                CacheHelper.Insert(user.LoginName, token, 7200);
                return(RedirectToAction("Index", "Manager"));
            }
            else
            {
                SetTempData("用户名或者密码不能为空!");
                return(RedirectToAction("Index"));
            }
        }