public async Task <ActionResult> Register(GroomerRegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Name, Email = model.Email }; model.Password = RandomPasswordService.GenerateRandomPassword(); // random password var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await UserManager.AddToRoleAsync(user.Id, "staff"); //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); //create groomer Groomer groomer = new Groomer(); UpdateModel(groomer); groomer.UserId = user.Id; repository.Insert(groomer); repository.Save(); // send email to groomer with user & password string subject = "Pet Grooming credentials"; ViewBag.UserName = model.Name; ViewBag.Password = model.Password; string message = RenderRazorViewToString("RegisterEmail", null); string emailResult; try { PetGroomingApplication.Services.Email.Send(model.Email, subject, message); emailResult = "The message was sent sussessfully"; } catch (Exception ex) { emailResult = ex.Message; } // log on disk string fileName = Path.GetFullPath(Server.MapPath(@"~/Data/Sent_credentials.txt")); using (StreamWriter file = new StreamWriter(fileName, true)) { file.WriteLine($"{DateTime.Now}\nUser name: {model.Name} \nPassword: {model.Password}\n-------------------"); } ViewBag.EmailResult = emailResult; return(RedirectToAction("SuccessRegister")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Register() { GroomerRegisterViewModel groomer = new GroomerRegisterViewModel(); return(View("Register", groomer)); }