Example #1
0
        public IActionResult SignIn(SignInViewModel model)
        {
            if (ModelState.IsValid)
            {
                BeerStoreUser existingUser = _signInManager.UserManager.FindByNameAsync(model.UserName).Result;
                //if user already exists
                if (existingUser != null)
                {
                    //check to see if their password is correct and attempt sign-in
                    Microsoft.AspNetCore.Identity.SignInResult passwordResult = _signInManager.CheckPasswordSignInAsync(existingUser, model.Password, false).Result;
                    if (passwordResult.Succeeded)
                    {
                        _signInManager.SignInAsync(existingUser, false).Wait();
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("PasswordIncorrect", "Username or password is incorrect");
                }
            }
            else
            {
                ModelState.AddModelError("UserDoesNotExist", "User or password does not exist");
            }

            return(View());
        }
Example #2
0
        public IActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                BeerStoreUser newUser = new BeerStoreUser
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber
                };

                IdentityResult creationResult = _signInManager.UserManager.CreateAsync(newUser).Result;

                if (creationResult.Succeeded)
                {
                    IdentityResult passwordResult = _signInManager.UserManager.AddPasswordAsync(newUser, model.Password).Result;
                    if (passwordResult.Succeeded)
                    {
                        _signInManager.SignInAsync(newUser, false).Wait();
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        foreach (var error in passwordResult.Errors)
                        {
                            ModelState.AddModelError(error.Code, error.Description);
                        }
                    }
                }
            }

            return(View());
        }