Example #1
0
        public async Task <IAuthenticationResponse> LogIn([FromBody] object data,
                                                          [FromServices] IAppSettings appSettings,
                                                          [FromServices] TokenConfigurations tokenConfigurations,
                                                          [FromServices] SigningConfigurations signingConfigurations)
        {
            var response = await _authFacade.LogIn(data, appSettings.KeyCrypto);

            if (response != null && response.Logged)
            {
                response.Token = TokenHelper.GenerateJwtToken(response.User.IdUsuario, tokenConfigurations, signingConfigurations);
            }

            return(response);
        }
Example #2
0
        public async Task <IActionResult> Login(LoginModel model,
                                                string returnUrl,
                                                [FromServices] TokenConfigurations tokenConfigurations,
                                                [FromServices] SigningConfigurations signingConfigurations)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            ModelState.Clear();


            try
            {
                var data = new AuthDataFromPassPhrase();
                data.UserIdentity = model.Login;
                data.KeyContent   = model.Senha;

                var response = await authService.LogIn(JsonConvert.SerializeObject(data), settings.KeyCrypto);

                if (response != null && response.Logged)
                {
                    response.Token              = TokenHelper.GenerateJwtToken(response.User.IdUsuario, tokenConfigurations, signingConfigurations);
                    SharedValues.Session        = SharedValues.Session ?? HttpContext.Session;
                    SharedValues.UsuarioLogado  = response.User;
                    SharedValues.SuccessMessage = string.Empty;
                    SharedValues.ErrorMessage   = string.Empty;

                    //Defina pelo menos um conjunto de claims...
                    var claims = new List <Claim>
                    {
                        //Atributos do usuário ...
                        new Claim(ClaimTypes.Name, response.User.Login),
                        new Claim(ClaimTypes.Role, "Admin"),
                        //new Claim("Nome", response.User.Nome),
                    };

                    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                    var authProperties = new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(2),
                        IsPersistent = true
                    };

                    //Loga de fato
                    var login = HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(identity), authProperties
                        );

                    return(RedirectToLocal(returnUrl));
                }
                else
                {
                    throw new Exception(string.Join(" - ", response.Errors.Select(r => r.ToString())));
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

            return(View());
        }