Beispiel #1
0
        public async Task <IActionResult> Create(CreateActionModel data
                                                 , [FromServices] SignInManager <AppUser> signInManager)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Home"));
            }

            string returnUrlParamName = CookieAuthenticationDefaults.ReturnUrlParameter;

            string returnUrl = Request.Query[returnUrlParamName].FirstOrDefault();


            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "There were validation errors");

                return(View("New", data.MapToNewViewModel(returnUrl)));
            }

            var result = await signInManager.PasswordSignInAsync(data.SigninForm?.Email
                                                                 , data.SigninForm?.Password
                                                                 , isPersistent : true
                                                                 , lockoutOnFailure : true);

            if (!result.Succeeded)
            {
                if (result.IsLockedOut)
                {
                    ModelState.AddModelError(string.Empty, "Locked out. Wait one minute before trying again");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login");
                }

                return(View("New", data.MapToNewViewModel(returnUrl)));
            }

            if (!Url.IsLocalUrl(returnUrl))
            {
                return(RedirectToAction("Index", "Home"));
            }

            return(LocalRedirect(returnUrl));
        }
Beispiel #2
0
        public async Task <IActionResult> Create(CreateActionModel data
                                                 , [FromServices] UserManager <AppUser> userManager)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, "There were validation errors");

                return(View("New", data.MapToNewViewModel()));
            }

            var user = new AppUser()
            {
                Email    = data.SignupForm?.Email,
                UserName = data.SignupForm?.Email
            };

            var result = await userManager.CreateAsync(user
                                                       , data.SignupForm?.Password);

            if (!result.Succeeded)
            {
                ModelState.AddModelError(string.Empty, "Failed to register user");

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }

                return(View("New", data.MapToNewViewModel()));
            }

            TempData.Add("Flash", "Sign up success");

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #3
0
        public IActionResult Create(CreateActionModel data
                                    , [FromServices] HouseRepository repo)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "There were validation errors");

                return(View("New", data.MapToNewViewModel()));
            }

            var house = data.House.MapToHouse();

            repo.Add(house);

            return(RedirectToAction("Show", new { house.Id }));
        }