public async void EnsureSeedData() { bool adminRoleExists = await _roleManager.RoleExistsAsync("Admin"); bool doctorRoleExists = await _roleManager.RoleExistsAsync("Doctor"); // Add roles: ADMIN DOCTOR if ( !adminRoleExists) await _roleManager.CreateAsync(new IdentityRole { Name = "Admin" }); if ( !doctorRoleExists) await _roleManager.CreateAsync(new IdentityRole { Name = "Doctor" }); ApplicationUser luis = new ApplicationUser { UserName = "******", FirstName = "Luis", LastName = "Lavieri", Email = "*****@*****.**" }; ApplicationUser manuel = new ApplicationUser { UserName = "******", FirstName = "Manuel", LastName = "Gonzalez", Email = "*****@*****.**" }; ApplicationUser ryan = new ApplicationUser { UserName = "******", FirstName = "Ryan", LastName = "Gonyon", Email = "*****@*****.**" }; bool luisExists = _userManager.Users.Any(x => x.UserName == "*****@*****.**"); bool manuelExists = _userManager.Users.Any(x => x.UserName == "*****@*****.**"); bool ryanExists = _userManager.Users.Any(x => x.UserName == "*****@*****.**"); // Add luis, manuel and ryan if they don't exists if( !luisExists ) await _userManager.CreateAsync(luis, "Hello1234*"); if (!manuelExists) await _userManager.CreateAsync(manuel, "Hello1234*"); if (!ryanExists) await _userManager.CreateAsync(ryan, "Hello1234*"); luis = _userManager.Users.First(x => x.UserName == "*****@*****.**"); manuel = _userManager.Users.First(x => x.UserName == "*****@*****.**"); ryan = _userManager.Users.First(x => x.UserName == "*****@*****.**"); bool luisAsAdmin = await _userManager.IsInRoleAsync(luis, "Admin"); bool manuelAsAdmin = await _userManager.IsInRoleAsync(manuel, "Admin"); bool ryanAsAdmin = await _userManager.IsInRoleAsync(ryan, "Admin"); if (!luisAsAdmin) await _userManager.AddToRoleAsync(luis, "Admin"); if (!manuelAsAdmin) await _userManager.AddToRoleAsync(manuel, "Admin"); if (!ryanAsAdmin) await _userManager.AddToRoleAsync(ryan, "Admin"); }
public async Task<IActionResult> InvitationRequest(InvitationViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Address = model.WorkAddress, FirstName = model.FirstName, LastName = model.LastName, City = model.City, DOB = model.DOB, Phone = model.Phone, Zip = model.Zip }; var appUser = await _userManager.FindByNameAsync(model.Email); if (appUser != null) { ModelState.AddModelError(string.Empty, "Sorry. This email is already registered"); return View(model); } var message = new AuthMessageSender.Message { Subject = "Register User", Body = "Please add me: <br>" + "First Name: " + user.FirstName + "<br>" + "Last Name: " + user.LastName + "<br>" + "DOB: " + user.DOB.ToString("d") + "<br>" + "Phone: " + user.Phone + "<br>" + "Email: " + user.Email + "<br>" + "Address: " + user.Address + "<br>" + "City: " + user.City + "<br>" + "Zip: " + user.Zip + "<br>" }; await _emailSender.SendEmailAsync(_userEmailAccount, message.Subject, message.Body, _userEmailAccount, _userEmailPassword); return View("ThankYou"); } // If we got this far, something failed, redisplay form return View(model); }
public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null) { if (User.IsSignedIn()) { return RedirectToAction(nameof(ManageController.Index), "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent: false); _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewData["ReturnUrl"] = returnUrl; return View(model); }
public async Task<IActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Address = model.WorkAddress, FirstName = model.FirstName, LastName = model.LastName, City = model.City, DOB = model.DOB, Phone = model.Phone, Zip = model.Zip }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Send an email with this link var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, Request.Scheme); var message = new AuthMessageSender.Message { Subject = "Confirm account", Body = "Please confirm " + user.FirstName + " " + user.LastName + " account by clicking: <a href='" + callbackUrl + "'>this link</a></br>" + "You can login using the following password: "******"</br>" + "Please, once logged in change your password for security reasons. Enjoy" }; await _emailSender.SendEmailAsync(model.Email, message.Subject, message.Body, _userEmailAccount, _userEmailPassword); message.Body = "You have been registered into 3DCytoFlow! Please, check your email to confirm your account"; _smsSender.SendSms(message, _sid, _authToken, _number, user.Phone); await _signInManager.SignInAsync(user, isPersistent: false); _logger.LogInformation(3, "User created a new account with password."); return RedirectToAction(nameof(HomeController.Index), "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { //Get the doctor's role var role = _db.UserRoles.First(i => i.Name.Equals("doctor")); var applicationUser = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(applicationUser, model.Password); //Create the user and assign role var user = new User { FirstName = model.FirstName, Middle = model.Middle, LastName = model.LastName, DOB = model.DOB, Login = model.Email, Password = applicationUser.PasswordHash, Email = model.Email, Phone = model.Phone, Address = model.WorkAddress, City = model.City, Zip = model.Zip, UserRole_Id = role.Id, UserRole = role }; //add it to the DB _db.Users.Add(user); if (result.Succeeded) { // Send an email with this link var code = await UserManager.GenerateEmailConfirmationTokenAsync(applicationUser.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = applicationUser.Id, code }, Request.Url.Scheme); EmailService.SendMail(new IdentityMessage { Subject = "Confirm " + user.FirstName + " " + user.LastName + " account", Body = "Please confirm " + user.FirstName + " " + user.LastName + " account by clicking: " + callbackUrl }); //save the changes to the db _db.SaveChanges(); return View("WaitConfirmation"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }