示例#1
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            User user = new User()
            {
                Email = model.Email, Password = model.Password
            };

            user = _db.GetUserDetails(user);

            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(model.Email, false);

                var    authTicket      = new FormsAuthenticationTicket(1, user.Email, DateTime.Now, DateTime.Now.AddMinutes(20), false, user.Roles);
                string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                var    authCookie      = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                HttpContext.Response.Cookies.Add(authCookie);
                if (user.Roles == "Secretary")
                {
                    return(RedirectToAction("Index", "Secretary"));
                }
                else if (user.Roles == "Director")
                {
                    return(RedirectToAction("Index", "Director"));
                }
                else if (user.Roles == "User")
                {
                    return(RedirectToAction("Index", "User"));
                }
                return(RedirectToAction("Index", "Home"));
            }

            else
            {
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }