using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using MTG_CollectionVerwaltung.Data; using System.Threading.Tasks; namespace MTG_CollectionVerwaltung.Areas.Identity.Pages.Account.Admin { [Authorize(Roles = "Admin")] public class ModifyUserModel : PageModel { private readonly UserManager _userManager; public ModifyUserModel(UserManager userManger) { _userManager = userManger; } [TempData] public string StatusMessage { get; set; } public ApplicationUser? userToModyfy { get; set; } public async Task OnGet(string id) { var CurrentUserID = _userManager.GetUserId(User); userToModyfy = await _userManager.FindByIdAsync(id); if (id == CurrentUserID) { return RedirectToPage("/Account/Admin/Index"); } return Page(); } public async Task OnPostResetPasswordAsync(string userId) { var userToModyfy = await _userManager.FindByIdAsync(userId); if (userToModyfy == null) { StatusMessage = "Error Benutzer konnte nicht gefunden werden."; return Page(); } var currentUserId = _userManager.GetUserId(User); if (userId == currentUserId) { StatusMessage = "Error Passwort des eigenen user kann nicht zurückgesetzt werden."; return Page(); } var standardPassword = "Password123!"; var token = await _userManager.GeneratePasswordResetTokenAsync(userToModyfy); var result = await _userManager.ResetPasswordAsync(userToModyfy, token, standardPassword); if (result.Succeeded) { StatusMessage = $"Passwort von {userToModyfy.UserName} zurückgesetzt."; } else { StatusMessage = "Error " + string.Join(", ", result.Errors.Select(e => e.Description)); } return Page(); } } }