Ejemplo n.º 1
0
        public async Task <IActionResult> Login([FromForm] User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var u = await _context.Users.FirstOrDefaultAsync(
                e => e.Login.ToLower() == user.Login.ToLower() && e.Password == user.Password);

            if (u != null)
            {
                var guid = _utils.GetGiudByUser();

                var token = new Token()
                {
                    Val         = guid.ToString(),
                    UserId      = u.Id,
                    Timecreated = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds
                };

                _context.Tokens.Add(token);
                await _context.SaveChangesAsync();

                return(Ok(new { token = guid, userid = u.Id, username = u.Login }));
            }

            return(StatusCode(403));
        }