コード例 #1
0
ファイル: HomeController.cs プロジェクト: nxybcce/FormAuth
        public ActionResult Index(User user)
        {
            if (!ModelState.IsValid)
            {
                return(View(user));
            }
            if (string.IsNullOrEmpty(user.Name))
            {
                ModelState.AddModelError("Name", "用户名不能为空");
                return(View(user));
            }
            if (string.IsNullOrEmpty(user.Pwd))
            {
                ModelState.AddModelError("Pwd", "密码不能为空");
                return(View(user));
            }
            if (db.CheckUser(user.Name, user.Pwd, out User userModel))
            {
                var role     = db.GetRoleById(userModel.RoleId);
                var userInfo = new UserInfo();
                userInfo.UserId   = role.Id;
                userInfo.RoleId   = role.Id;
                userInfo.UserName = role.Name;
                userInfo.PList    = role.PList;
                string perListKey = string.Format("userPermission_{0}", role.Id);
                if (HttpRuntime.Cache.Get(perListKey) == null)
                {
                    HttpRuntime.Cache.Insert(perListKey, role.PList);
                }
                var cookie = UserFormsPrincipal <UserInfo> .SingIn(user.Name, userInfo, 100);

                return(RedirectToAction("Welcome"));
            }
            else
            {
                ModelState.AddModelError("Error", "用户名或者密码错误");
                return(View(user));
            }
        }