예제 #1
0
        public ActionResult Login(LoginViewModel login) //Login [POST]
        {
            if (ModelState.IsValid)
            {
                //Auto mapper
                var     mapAccount = new MapperConfiguration(configExpression => { configExpression.CreateMap <LoginViewModel, Account>(); });
                IMapper mapper     = mapAccount.CreateMapper();
                var     account    = mapper.Map <LoginViewModel, Account>(login);

                Account accountDetails = accountBL.CheckUser(account); //Method call to check user.
                if (accountDetails != null)
                {
                    FormsAuthentication.SetAuthCookie(accountDetails.Name, false);
                    var    authTicket      = new FormsAuthenticationTicket(1, accountDetails.Name, DateTime.Now, DateTime.Now.AddMinutes(20), false, accountDetails.Role); //Authentication ticket is created to track session of user.
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    var    authCookie      = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    HttpContext.Response.Cookies.Add(authCookie); //Session is added in cookies
                    return(RedirectToAction("Index", "Movie"));
                }
                else
                {
                    TempData["LoginErrorMessage"] = "Invalid Username or Password";
                }
            }
            return(View());
        }