Esempio n. 1
0
        public async Task <IActionResult> PostUser(
            [FromQuery] string username,
            [FromQuery] string email,
            [FromQuery] string password)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { WasSuccessful = false, Message = "Invalid model state." }));
            }

            email    = email.ToLower();
            username = username.ToLower();

            // Validate that the user doesn't currently exist...
            bool alreadyExists = false;

            // Initiate user...
            User user = new User()
            {
                UserName = username,
                Salt     = Helper.GenerateSalt()
            };

            user.Id             = 0; // Must be zero to ensure that the database sets it
            user.HashedPassword = Helper.HashPassword(password, user.Salt);
            _context.User.Add(user);

            await _context.SaveChangesAsync();

            return(Json(new { WasSuccessful = true }));
        }
        public async Task <IActionResult> Token([FromQuery] string username, [FromQuery] string password)
        {
            if (username == null || password == null)
            {
                int a = 10;
            }

            /////////////////////////////////
            // Validate Username and Password

            User user = await _context.User.FirstOrDefaultAsync(u => u.UserName.Equals(username));

            if (user == null)
            {
                return(Json(new { LoginSuccessful = false, Message = "Username not found!" }));
            }

            String hashedPassword = Helper.HashPassword(password, user.Salt);

            if (!hashedPassword.Equals(user.HashedPassword))
            {
                return(Json(new { LoginSuccessful = false, Message = "Hashed passwords don't match!" }));
            }

            ///////////////////
            // Generate a token

            // TODO: Beef up security
            char c = 'a';

            char[] randomCharArray = new char[30];
            Random r = new Random();

            for (int i = 0; i < 30; i++)
            {
                randomCharArray[i] = (char)(c + r.Next(0, 25));
            }

            Console.WriteLine(randomCharArray);

            string tokenValue = new string(randomCharArray);

            Token newToken = new Token()
            {
                CorrespondingLoginId = user.Id,
                Value = new string(randomCharArray)
            };

            //////////////////////////////
            // Write token to the database
            _context.Token.Add(newToken);
            await _context.SaveChangesAsync();

            return(Json(new { LoginSuccessful = true, Token = tokenValue, UserId = user.Id }));
        }
Esempio n. 3
0
		public async Task<Beacon> AddAsync(BeaconDto beaconDto)
		{
			var beacon = _converter.ConvertDtoToBeacon(beaconDto);
			_context.Add(beacon);
			await _context.SaveChangesAsync();
			return beacon;
		}
Esempio n. 4
0
 public async Task Seed()
 {
     if (!_context.Beacons.Any())
     {
         _context.AddRange(getInitData(Guid.NewGuid()));
         await _context.SaveChangesAsync();
     }
 }
Esempio n. 5
0
        public async Task <IActionResult> DownloadUpdates([FromQuery] decimal lat, [FromQuery] decimal lng, [FromQuery] DateTime lastUpdatedTime)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { WasSuccessful = false, Message = "Invalid model state for DownloadUpdates." }));
            }

            // Download all events where the event was updated after the last client's updated time
            GeoBox gb = CalculateBoundingGeoBox(lat, lng, Policy.SURROUNDING_GEOBOX_SIDE_LENGTH);

            var @events = await _context.Event.Where(e => (
                                                         e.Latitude <gb.MaxLatitude &&
                                                                     e.Latitude> gb.MinLatitude &&
                                                         e.Longitude <gb.MaxLongitude &&
                                                                      e.Longitude> gb.MinLongitude &&
                                                         e.TimeLastUpdated > lastUpdatedTime
                                                         )).ToListAsync();

            // Determine if any of these events need to be deleted...
            var eventsToDelete = @events.Where(Policy.ShouldMarkEventForDeletion);

            foreach (Event e in eventsToDelete)
            {
                e.Deleted         = true;
                e.TimeLastUpdated = DateTime.UtcNow;
            }

            _context.UpdateRange(eventsToDelete);
            await _context.SaveChangesAsync();

            return(Json(new { WasSuccessful = true, Events = @events, CurrentServerTime = DateTime.UtcNow }));
        }
        public async Task <IActionResult> DeleteToken([FromRoute] string id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var token = await _context.Token.SingleOrDefaultAsync(m => m.Value == id);

            if (token == null)
            {
                return(NotFound());
            }

            _context.Token.Remove(token);
            await _context.SaveChangesAsync();

            return(Ok(token));
        }