Ejemplo n.º 1
0
        public IActionResult Authenticate([FromBody] UserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (model.UserName == "Alex")
            {
                /*throw new ArgumentException(
                 *  $"We can't login user with name {model.UserName}");*/
            }

            var userCredentials = new User()
            {
                UserName = model.UserName,
                Password = model.Password.CreateMD5()
            };

            _processTimer.Start();
            var user = _repository.GetAsync(userCredentials, model.ClientId);

            _processTimer.Stop();

            Log.Information($"Duration of query was {_processTimer.ElapsedMilliseconds}ms and query name was GetAsync");

            if (user.Result != null)
            {
                _processTimer.Start();
                var tokenString = _service.GenerateJWTToken(model.UserName);
                _processTimer.Stop();
                Log.Information($"Duration of encryption of JWT token was {_processTimer.ElapsedMilliseconds}ms");

                return(Ok(new
                {
                    token = tokenString
                }));
            }

            return(Unauthorized());
        }