public async Task<HttpResponseMessage> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState); } if (model.ProfileImageURL != null) { string storageString = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ToString(); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a previously created container. CloudBlobContainer container = blobClient.GetContainerReference("profile-pictures"); //Create the "images" container if it doesn't already exist. container.CreateIfNotExists(); // Retrieve reference to a blob with this name blockBlob = container.GetBlockBlobReference("profile_image_" + Guid.NewGuid()); byte[] imageBytes = Convert.FromBase64String(model.ProfileImageURL); // create or overwrite the blob named "image_" and the current date and time blockBlob.UploadFromByteArray(imageBytes, 0, imageBytes.Length); model.ProfileImageURL = getImageURL(); } else { model.ProfileImageURL = "https://posseup.blob.core.windows.net/profile-pictures/05-512.png"; } var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, ProfileImageURL = model.ProfileImageURL }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return Request.CreateResponse(HttpStatusCode.BadRequest, result); } var myMessage = new SendGridMessage(); myMessage.From = new MailAddress("*****@*****.**"); myMessage.AddTo(string.Format(@"{0} <{1}>", user.UserName, user.Email)); myMessage.Subject = "Welcome to Posse Up!"; myMessage.Html = "<p>Hello World!</p>"; myMessage.Text = "Hello World plain text!"; await SendEmail(myMessage); // Send the email. return Request.CreateResponse(HttpStatusCode.OK, model); }
public async Task<IHttpActionResult> ChangeUsername(RegisterBindingModel model) { if (ModelState.IsValid) { var existingUsername = db.Users.Where(x => x.UserName.Equals(model.UserName, StringComparison.OrdinalIgnoreCase)).ToList(); //finds any users with existing username if (existingUsername.Count < 1) //the chosen username doesn't exist { var u = UserManager.FindByEmail(model.Email); //finds the user details based on email if (u != null) //if the email checks out with an existing user { bool passwordCheck = await UserManager.CheckPasswordAsync(u, model.Password); //check the password is correct to the user if (passwordCheck) { u.UserName = model.UserName; UserManager.Update(u); return Json(new { success = true }); } return Json(new { success = false, cause = "Incorrect password supplied" }); } return Json(new { success = false, cause = "User not found" }); } return Json(new { success = false, cause = "Username is already taken" }); } return BadRequest(ModelState); }