public async Task <IActionResult> POST([FromBody] Reservation newReservation)
        {
            ModelState.Remove("User");
            User user = await _context.User.Where(u => u.UserName == User.Identity.Name).SingleOrDefaultAsync();

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            newReservation.User = user;
            _context.Reservation.Add(newReservation);

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (ReservationExists(newReservation.ReservationId))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtRoute("GetSingleNapSpace", new { id = newReservation.ReservationId }, newReservation));
        }
Beispiel #2
0
        public async Task <IActionResult> Create(string username, string password)
        {
            // Check simplistic username and password validation rules
            bool isValid = IsValidUserAndPasswordCombination(username, password);

            if (isValid)
            {
                // Does the user already exist?
                User user = _context.User.SingleOrDefault(u => u.UserName == username);

                if (user != null)
                {
                    // Found the user, verify credentials
                    var result = await _signInManager.PasswordSignInAsync(username, password, false, lockoutOnFailure : false);

                    // Password is correct, generate token and return it
                    if (result.Succeeded)
                    {
                        return(new ObjectResult(GenerateToken(user.UserName)));
                    }
                }
                else
                {
                    var userstore = new UserStore <User>(_context);

                    // User does not exist, create one
                    user = new User
                    {
                        FirstName          = "Generic",
                        LastName           = "User",
                        UserName           = username,
                        NormalizedUserName = username.ToUpper(),
                        Email           = username,
                        NormalizedEmail = username.ToUpper(),
                        EmailConfirmed  = true,
                        LockoutEnabled  = false,
                        SecurityStamp   = Guid.NewGuid().ToString("D")
                    };
                    var passwordHash = new PasswordHasher <User>();
                    user.PasswordHash = passwordHash.HashPassword(user, password);
                    await userstore.CreateAsync(user);

                    _context.SaveChanges();
                    return(new ObjectResult(GenerateToken(user.UserName)));
                }
            }
            return(BadRequest());
        }