public HomeLocation Post(string username, string latitude, string longitude) { ApplicationUser user = _ctx.Users.Where(u => u.UserName == username).Single(); // create home location HomeLocation home = new HomeLocation() { User = user, Lat = Convert.ToDouble(latitude), Lon = Convert.ToDouble(longitude) }; // check for existing record var existingRecords = _ctx.HomeLocation.Where(l => l.User == user); // remove the old user records if (existingRecords.Count() > 0) { foreach (var e in existingRecords) { _ctx.HomeLocation.Remove(e); } } _ctx.HomeLocation.Add(home); _ctx.SaveChanges(); home.User = null; return(home); }
public async Task <IActionResult> Create(string username, string password) { // Hard coding role here for now string role = "Administrator"; // Check simplistic username and password validation rules bool isValid = IsValidUserAndPasswordCombination(username, password); if (isValid) { // Does the user already exist? ApplicationUser user = _context.ApplicationUser.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, role))); } } else { var userstore = new UserStore <ApplicationUser>(_context); // ApplicationUser does not exist, create one user = new ApplicationUser { UserName = username, NormalizedUserName = username.ToUpper(), Email = username, NormalizedEmail = username.ToUpper(), EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString("D") }; var passwordHash = new PasswordHasher <ApplicationUser>(); user.PasswordHash = passwordHash.HashPassword(user, password); await userstore.CreateAsync(user); await userstore.AddToRoleAsync(user, role); _context.SaveChanges(); return(new ObjectResult(GenerateToken(user.UserName, role))); } } return(BadRequest()); }