72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
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<ApplicationUser> _userManager;
|
|
|
|
public ModifyUserModel(UserManager<ApplicationUser> userManger)
|
|
{
|
|
_userManager = userManger;
|
|
}
|
|
|
|
[TempData]
|
|
public string StatusMessage { get; set; }
|
|
|
|
public ApplicationUser? userToModyfy { get; set; }
|
|
|
|
public async Task<IActionResult> 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<IActionResult> 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();
|
|
}
|
|
}
|
|
}
|