Esempio n. 1
0
        public async Task <IActionResult> SignIn(SignInViewModel model, string returnUrl = null)
        {
            // Get user from database.
            var getUserResult = await _mediator.Send(
                new GetUserByEmail(model.UserName));

            if (!getUserResult.Succeeded)
            {
                ModelState.AddErrorsFromCommonResult(getUserResult);
                return(View());
            }

            // Login user to create app cookie, which is used for authentication on next request.
            var signInResult = await _mediator.Send(
                new SignInByPassword(getUserResult.Data, model.Password, model.RememberMe, false));

            if (!signInResult.Succeeded)
            {
                ModelState.AddErrorsFromCommonResult(signInResult);
                return(View(model));
            }

            _logger.Information("LOGIN: User {Name} logged in with a local password.", model.UserName);

            // Send user to the page they were requesting when the SignIn was triggered, or home.
            return(RedirectToLocal(returnUrl));
        }
Esempio n. 2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            AppUser user = new AppUser(
                model.Email,
                model.FirstName,
                model.LastName,
                model.PrimaryPhone);

            CommonResult registerUserResult = await _mediator.Send(
                new RegisterLocalUser(
                    user,
                    model.Password));

            if (!registerUserResult.Succeeded)
            {
                ModelState.AddErrorsFromCommonResult(registerUserResult);
                return(View(model));
            }

            _logger.Information("NEW USER: User {user} registered a new local account", model.Email);

            // Automatically log the user in by passing execution to the SignIn action.

            return(await SignIn(new SignInViewModel()
            {
                UserName = model.Email,
                Password = model.Password,
                RememberMe = model.RememberMe
            },
                                returnUrl));
        }