Ejemplo n.º 1
0
        public async Task <ActionResult> Login(LoginModel loginModel)
        {
            // Return to the login page if the view model was invalid (incomplete data)
            if (!ModelState.IsValid)
            {
                return(View("LoginPage"));
            }

            User user = await userManager.FindAsync(loginModel.UserName, loginModel.Password);

            if (user != null)
            {
                ClaimsIdentity identity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

                IOwinContext context = Request.GetOwinContext();
                context.Authentication.SignIn(identity);

                user.IsLoggedIn = 1;
                await userManager.UpdateAsync(user);

                if (string.IsNullOrEmpty(loginModel.RequestedUrl) || !Url.IsLocalUrl(loginModel.RequestedUrl))
                {
                    return(Redirect(Url.Action("Home", "Home")));
                }

                ViewBag.Clear();
                return(Redirect(loginModel.RequestedUrl));
            }

            ViewBag.Error = "Invalid credentials. Try again.";
            return(View("LoginPage"));
        }