Implemented ability to reset a user's password.

This commit is contained in:
Joseph Milazzo 2021-01-21 11:15:42 -06:00
parent 6309ae9dd3
commit 8220709b4c
4 changed files with 31 additions and 4 deletions

View File

@ -4,8 +4,10 @@ using System.Threading.Tasks;
using API.Constants;
using API.DTOs;
using API.Entities;
using API.Extensions;
using API.Interfaces;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -36,6 +38,21 @@ namespace API.Controllers
_mapper = mapper;
}
[Authorize(Policy = "RequireAdminRole")]
[HttpPost("reset-password")]
public async Task<ActionResult> UpdatePassword(ResetPasswordDto resetPasswordDto)
{
_logger.LogInformation($"{User.GetUsername()} is changing {resetPasswordDto.UserName}'s password.");
var user = await _userManager.Users.SingleAsync(x => x.UserName == resetPasswordDto.UserName);
var result = await _userManager.RemovePasswordAsync(user);
if (!result.Succeeded) return BadRequest("Unable to update password");
result = await _userManager.AddPasswordAsync(user, resetPasswordDto.Password);
if (!result.Succeeded) return BadRequest("Unable to update password");
return Ok($"{resetPasswordDto.UserName}'s Password has been reset.");
}
[HttpPost("register")]
public async Task<ActionResult<UserDto>> Register(RegisterDto registerDto)
{

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace API.DTOs
{
public class ResetPasswordDto
{
[Required]
public string UserName { get; init; }
[Required]
[StringLength(8, MinimumLength = 4)]
public string Password { get; init; }
}
}

View File

@ -25,7 +25,6 @@ namespace API.Middleware
public async Task InvokeAsync(HttpContext context)
{
_logger.LogError("The middleware called");
try
{
await _next(context); // downstream middlewares or http call

View File

@ -42,10 +42,8 @@ namespace API
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
app.UseHangfireDashboard();
}
app.UseHangfireDashboard();
app.UseHttpsRedirection();