Exemplo n.º 1
0
        public async Task <IActionResult> CustomLogin([FromBody] UserModel userModel)
        {
            var user = userDbContext.Users.FirstOrDefault(u => u.Username == userModel.Username);

            if (user != null)
            {
                if (user.Password == userModel.Password.Sha256())
                {
                    var token = new Entities.Token
                    {
                        Id         = Guid.NewGuid().ToString(),
                        Expiration = DateTime.Now + TimeSpan.FromMinutes(30),
                        Owner      = userModel.Username,
                        Role       = user.Role
                    };
                    tokenDbContext.Add(token);
                    tokenDbContext.SaveChanges();
                    return(Ok(token.Id));
                }
                else
                {
                    return(StatusCode(500, "Wrong password"));
                }
            }
            return(StatusCode(500, "User not found"));
        }