예제 #1
0
        public async Task <IActionResult> Login(UserLogin login)
        {
            if (!ModelState.IsValid)
            {
                return(View(login));
            }

            var loginService = new LoginRegisterService();

            if (LoginRegisterService.IsLoginValid(login.Username, login.Password))
            {
                login.IsSuccess = loginService.Login(login.Username, login.Password);

                if (!login.IsSuccess)
                {
                    ViewBag.ErrorMessage = "Your credentials were not valid. Please try again.";
                }
                else
                {
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Name, login.Username));

                    var principal = new ClaimsPrincipal(identity);
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                ViewBag.ErrorMessage = "Your credentials were not valid. Please try again.";
            }

            return(View(login));
        }
예제 #2
0
        public async Task <IActionResult> Register(UserRegistration register)
        {
            if (!ModelState.IsValid)
            {
                return(View(register));
            }

            var loginService = new LoginRegisterService();

            if (LoginRegisterService.IsRegistrationValid(register.Password, register.ConfirmPassword, register.Email, register.FriendlyName))
            {
                register.IsSuccess = loginService.RegisterNewUser(register.Email, register.Password, register.FriendlyName);

                if (!register.IsSuccess)
                {
                    ViewBag.ErrorMessage = "Your account could not be created. Please try again.";
                }
                else
                {
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Name, register.Email));

                    var principal = new ClaimsPrincipal(identity);
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                ViewBag.ErrorMessage = register.Password != register.ConfirmPassword ? "Your password and confirmation did not match." : "Your account could not be created. Please try again.";
            }

            return(View(register));
        }