Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] Login loginModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var userData = await service.GetByLogin(loginModel.Username, loginModel.Password);

                    var userId = userData == null ? "-1" : userData.Username;
                    if (userId == "-1")
                    {
                        return(Unauthorized());
                    }

                    var claims      = GetClaims(loginModel, userData);
                    var expires     = DateTime.UtcNow.AddMinutes(2);
                    var token       = GetToken(claims, expires);
                    var tokenString = new JwtSecurityTokenHandler().WriteToken(token);

                    var z = await serviceZabbix.Login("U002795", "b4tata#123");

                    userData.Jwt         = tokenString;
                    userData.TokenZabbix = z.Token;
                    userData.IdZabbix    = z.Id;
                    await service.Save(userData);

                    Startup.AddUser(new LoggedUser {
                        Username = loginModel.Username, LastCall = DateTime.Now
                    });
                    return(Ok(new { token = tokenString }));
                }
                catch (Exception ex)
                {
                    return(StatusCode(500, ex.Message));
                }
            }

            return(BadRequest());
        }