public RegisterMedic() { InitializeComponent(); DataContext = new RegisterMedicViewModel(); }
public async Task <IActionResult> RegisterMedic(RegisterMedicViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { int ok = 1; // 1 = It's ok foreach (var x in _databaseService.Users.ToList()) { if (x.Email.Equals(model.Email)) { // Email already used ok = 3; } else if (model.BirthDate > DateTime.Now) { // Person must be already born ok = 5; } else if (((int)model.BirthDate.Year > (int)((int)DateTime.Now.Year - 24)) && (model.BirthDate <= DateTime.Now)) { // An < 24 years old person can't be an medic ok = 6; } else if ((int)model.BirthDate.Year < (int)((int)DateTime.Now.Year - 250)) // There is not person older then 250 years { ok = 4; } } if (ok == 1) { var user = new ApplicationUser { UserName = model.Email, FirstName = model.FirstName, LastName = model.LastName, BirthDate = model.BirthDate, Email = model.Email, PhoneNumber = model.PhoneNumber, Title = model.Title, CabinetAddress = model.CabinetAddress, ActiveAccount = true, CreatedDate = DateTime.Now }; user.FirstName = user.FirstName.Encrypt(); user.LastName = user.LastName.Encrypt(); user.PhoneNumber = user.PhoneNumber.Encrypt(); user.Title = user.Title.Encrypt(); user.CabinetAddress = user.CabinetAddress.Encrypt(); var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var result2 = await _userManager.AddToRoleAsync(user, UserRoles.Medic.ToString()); if (result2.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, user.FirstName.Decrypt() + " " + user.LastName.Decrypt(), callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToLocal(returnUrl)); } } AddErrors(result); } if (ok == 3) { // Email AddStringErrors("This mail was already used!"); } if (ok == 4) { // age > 250 years AddStringErrors("There is no existing person with this age!"); } if (ok == 5) { // unborn yet AddStringErrors("Create your account when u'll be born!(Inserted date is from the future)"); } if (ok == 6) { // < 24 years old person can't be an medic AddStringErrors("An person which is less then 24 can't be an doctor"); } } // If we got this far, something failed, redisplay form return(View(model)); }