Exemplo n.º 1
0
        public async Task <IActionResult> SignIn(SignInFormModel input, [FromServices] ValidateUser useCaseValidateUser)
        {
            if (!ModelState.IsValid)
            {
                return(View(new SignInViewModel
                {
                    ReturnUrl = input.ReturnUrl,
                }));
            }

            var results = await useCaseValidateUser.ExecuteAsync(input);

            if (results.Any())
            {
                foreach (var error in results)
                {
                    ModelState.AddModelError(error.Key, error.Message);
                }
                return(View(new SignInViewModel
                {
                    ReturnUrl = input.ReturnUrl,
                }));
            }

            var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

            identity.AddClaims(new[]
            {
                new Claim(ClaimTypes.Name, _securityConfig.User),
                new Claim(ClaimTypes.NameIdentifier, _securityConfig.User)
            });

            await HttpContext.SignInAsync(new ClaimsPrincipal(identity));

            return(Redirect(input.ReturnUrl));
        }