Example #1
0
        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 async Task <IHttpActionResult> RecoveryPassword(string email)
        {
            var user = await AppUserManager.FindByEmailAsync(email);

            if (user == null)
            {
                Logger.ServiceLog.Warn($"Пользователь с email {email} не зарегистрирован.");
                return(GetErrorFromModel("email", $"Пользователь с email {email} не зарегистрирован."));
            }
            var recoveryToken = AppUserManager.GeneratePasswordResetTokenAsync(user.Id);
            var code          = HttpUtility.UrlEncode(recoveryToken.Result);
            var password      = RandomPasswordService.Generate(6);
            var callBackUri   = new Uri(Url.Link("RecoveryPasswordRoute", new { userId = user.Id, code, newPassword = password }));

            await AppUserManager.SendEmailAsync(user.Id, "Восстановление пароля",
                                                "Ваш новый пароль: <strong>" + password + "</strong> <br> Для его подтверждения перейдите по ссылке <a href=\"" + callBackUri + "\">ССылка</a>");

            Logger.ServiceLog.Info($"Пользователю на email {email} успешно выслана инструкция по восстановдению пароля.");
            return(Ok("Инструкция по смене пароля выслана на почту"));
        }