Пример #1
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByEmailAsync(model.Email);

                if (user != null && (await userManager.IsEmailConfirmedAsync(user)))
                {
                    //generate password reset token
                    string token = await userManager.GeneratePasswordResetTokenAsync(user);

                    //generate the link
                    string confirmationLink = Url.Action("ResetPassword", "Account", new { email = model.Email, token = token }, Request.Scheme);
                    string folder           = Path.Combine(webHostEnvironment.WebRootPath, "PasswordReset");
                    string filename         = "ResetPassword_" + model.Email + "_" + DateTime.Today.Day + "_" + DateTime.Today.Month + "_" + DateTime.Today.Year + ".txt";
                    //log it
                    var log = new MyLogger(folder, filename);
                    log.LogToFile(confirmationLink);
                    ViewBag.ErrorTitle   = "Password Reset Link";
                    ViewBag.ErrorMessage = "We have emailed you a link to reset your password";
                    return(View("Error"));
                }
                ViewBag.ErrorTitle   = "Not Found!";
                ViewBag.ErrorMessage = "Sorry your account was not found or your email has not been confirmed";
                return(View("Error"));
            }
            ModelState.AddModelError("", "Check email");
            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> Register(RegistrationViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = null;
                if (model.photo != null)
                {
                    string uploadfolder = Path.Combine(webHostEnvironment.WebRootPath, "ProfilePhoto");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + model.photo.FileName;
                    string filepath = Path.Combine(uploadfolder, uniqueFileName);
                    model.photo.CopyTo(new FileStream(filepath, FileMode.Create));
                }
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    PhoneNumber = model.PhoneNumber,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    Address     = model.Address,
                    Gender      = model.Gender,
                    State       = model.State,
                    PhotoPath   = uniqueFileName,
                };
                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token = token }, Request.Scheme);

                    string folder   = Path.Combine(webHostEnvironment.WebRootPath, "EmailTokens");
                    string filename = "EmailToken_" + model.Email + "_" + DateTime.Today.Day + "_" + DateTime.Today.Month + "_" + DateTime.Today.Year + ".txt";
                    //log to file
                    MyLogger myLogger = new MyLogger(folder, filename);
                    myLogger.LogToFile(confirmationUrl);
                    return(View("RegistrationSuccessful"));
                    //await signInManager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("index", "Home");
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(model));
        }