Esempio n. 1
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _platoUserManager.GetForgotPasswordUserAsync(model.UserIdentifier);

                if (result.Succeeded)
                {
                    var user = result.Response;
                    if (user != null)
                    {
                        // Ensure account has been confirmed
                        if (await _userManager.IsEmailConfirmedAsync(user))
                        {
                            user.ResetToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(user.ResetToken));
                            var emailResult = await _userEmails.SendPasswordResetTokenAsync(user);

                            if (!emailResult.Succeeded)
                            {
                                foreach (var error in emailResult.Errors)
                                {
                                    ViewData.ModelState.AddModelError(string.Empty, error.Description);
                                }

                                return(View(model));
                            }
                        }
                    }
                }
            }

            return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
        }
Esempio n. 2
0
        public async Task <IActionResult> EditPassword(string id)
        {
            // Ensure we have permission
            if (!await _authorizationService.AuthorizeAsync(User,
                                                            Permissions.ResetUserPasswords))
            {
                return(Unauthorized());
            }

            // Get user
            var user = await _userManager.FindByIdAsync(id);

            // Ensure user exists
            if (user == null)
            {
                return(NotFound());
            }

            // Add user to the context
            HttpContext.Items[typeof(User)] = user;

            // Build breadcrumb
            _breadCrumbManager.Configure(builder =>
            {
                builder.Add(S["Home"], home => home
                            .Action("Index", "Admin", "Plato.Admin")
                            .LocalNav()
                            ).Add(S["Users"], channels => channels
                                  .Action("Index", "Admin", "Plato.Users")
                                  .LocalNav()
                                  ).Add(S[user.DisplayName], channels => channels
                                        .Action("Edit", "Admin", "Plato.Users", new RouteValueDictionary()
                {
                    ["Id"] = user.Id.ToString()
                })
                                        .LocalNav()
                                        )
                .Add(S["Edit Password"]);
            });

            // Get reset token
            var resetToken = "";
            var result     = await _platoUserManager.GetForgotPasswordUserAsync(user.Email);

            if (result.Succeeded)
            {
                if (result.Response != null)
                {
                    resetToken = result.Response.ResetToken;
                }
            }

            // Return view
            return(View(new EditPasswordViewModel
            {
                Id = user.Id.ToString(),
                Email = user.Email,
                ResetToken = resetToken
            }));
        }
Esempio n. 3
0
        public async Task <IActionResult> ResetPassword()
        {
            // Get user
            var user = await _contextFacade.GetAuthenticatedUserAsync();

            // Ensure user exists
            if (user == null)
            {
                return(NotFound());
            }

            var result = await _platoUserManager.GetForgotPasswordUserAsync(user.UserName);

            if (result.Succeeded)
            {
                // Ensure account has been confirmed
                if (await _userManager.IsEmailConfirmedAsync(user))
                {
                    user.ResetToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(result.Response.ResetToken));
                    var emailResult = await _userEmails.SendPasswordResetTokenAsync(result.Response);

                    if (emailResult.Succeeded)
                    {
                        _alerter.Success(T["Check your email. We've sent you a password reset link!"]);
                    }
                    else
                    {
                        foreach (var error in emailResult.Errors)
                        {
                            _alerter.Danger(T[error.Description]);
                        }
                    }
                }
                else
                {
                    _alerter.Danger(T["You must confirm your email before you can reset your password!"]);
                }
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    _alerter.Danger(T[error.Description]);
                    //ViewData.ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(RedirectToAction(nameof(EditAccount)));
        }