public async Task <ActionResult> Register(DonorRegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.RegisterViewModel.UserName, Email = model.RegisterViewModel.Email }; user.CreatedOn = DateTime.Now; var result = await UserManager.CreateAsync(user, model.RegisterViewModel.Password); if (result.Succeeded) { this.UserManager.AddToRole(user.Id, GlobalConstants.DonorRoleName); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); Donor donor = new Donor(); donor.ApplicationUserId = user.Id; Mapper.Map <DonorRegisterViewModel, Donor>(model, donor); this.donorProfileService.Add(donor); return(RedirectToAction("Index", "Home", new { area = "" })); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public bool Register(DonorRegisterViewModel model) { bool result = false; if (model != null) { var repo = unitOfWork.GetRepository <Donors>(); var donor = new Donors { Email = model.Email, Password = model.Password, Age = model.Age, City = model.City, Country = model.Country, FirstName = model.FirstName, LastName = model.LastName, MiddleName = model.MiddleName, Phone = model.Phone, Pincode = model.Pincode, State = model.State }; repo.Insert(donor); unitOfWork.Save(); result = true; } return(result); }
public ActionResult RegisterDonor(DonorRegisterViewModel user) { try { if (ModelState.IsValid) { var encryptedPassword = CustomEncrypt.Encrypt(user.Password); if (db.AspNetUsers.FirstOrDefault(o => o.Email == user.Email) != null || db.Donors.FirstOrDefault(o => o.cnp == user.CNP) != null) { TempData["UserAlreadyExists"] = "This donor already exists"; return(View(user)); } var donor = new Donor(); donor.cnp = user.CNP; donor.firstName = user.firstName; donor.lastName = user.lastName; donor.birthDate = user.birthDate; donor.address = user.address; donor.email = user.Email; donor.phoneNumber = user.phoneNumber; if (donor.idBlood != 9) { donor.idBlood = user.idBlood; } db.Donors.Add(donor); var userDb = new AspNetUser(); userDb.Email = user.Email; userDb.Password = encryptedPassword; userDb.idRole = 1; db.AspNetUsers.Add(userDb); db.SaveChanges(); TempData["SuccessRegistration"] = "You registered successfully"; return(RedirectToAction("Login", "Account")); } else { return(View(user)); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"", ve.PropertyName, eve.Entry.CurrentValues.GetValue <object>(ve.PropertyName), ve.ErrorMessage); } } throw; } }
public IActionResult Register([FromBody] DonorRegisterViewModel model) { var result = accountService.Register(model); if (result) { return(Ok("registered successfully")); } return(BadRequest()); }