Example #1
0
        public async Task <IActionResult> Login(string returnUrl, LoginViewModel model)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Profile"));
            }

            if (ModelState.IsValid)
            {
                UserModel user = await ManageUser.FindByEmailAsync(model.Email);

                if (user != null && await ManageUser.CheckPasswordAsync(user, model.Password))
                {
                    if (await ManageUser.IsEmailConfirmedAsync(user))
                    {
                        var result = await SignManager.PasswordSignInAsync(user, model.Password, true, false);

                        if (result.Succeeded)
                        {
                            if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                            {
                                return(LocalRedirect(returnUrl));
                            }
                            else
                            {
                                return(RedirectToAction("Index", "Home"));
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("Error", result.ToString());
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Error", "Email is not verified.");
                    }
                }
                else
                {
                    ModelState.AddModelError("Error", "Failed : Invalid Login Attempt");
                }
            }

            return(View(model));
        }