Ejemplo n.º 1
0
        public AuthenticateResponse Authenticate(AuthenticateRequest model, string ipAddress)
        {
            var user = _webApiDbcontext.User.SingleOrDefault(x => x.Username == model.Username && x.Password == model.Password);

            // return null if user not found
            if (user == null)
            {
                return(null);
            }

            // authentication successful so generate jwt and refresh tokens
            var jwtToken     = GenerateJwtToken(user);
            var refreshToken = GenerateRefreshToken(ipAddress);

            if (user.RefreshTokens == null)
            {
                user.RefreshTokens = new List <RefreshToken>();
            }
            // save refresh token
            user.RefreshTokens.Add(refreshToken);
            _webApiDbcontext.Update(user);
            _webApiDbcontext.SaveChanges();

            return(new AuthenticateResponse(user, jwtToken, refreshToken.Token));
        }
        public async Task <ActionResult> Put(City city)
        {
            try
            {
                var existingCity = _context.Cities.FirstOrDefault(s => s.Id == city.Id);

                if (existingCity == null)
                {
                    throw new Exception("not existing city");
                }

                existingCity.CountryId = city.CountryId;
                existingCity.Name      = city.Name;

                _context.Update(existingCity);

                await _context.SaveChangesAsync();


                return(NoContent());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }