Метод контроллера:
Код: Выделить всё
[HttpPut]
[Route("api/vehicle")]
[SwaggerResponse((int)HttpStatusCode.Accepted)]
public async Task VehicleModify([FromBody] VehicleModel vehicleModel)
{
bool userIsAdmin = JwtMiddleware.IsUserInRoleForHttpContext(_httpContextAccessor, "Admin");
if (!userIsAdmin)
{
_logger.LogInformation("VehicleModify {@Plate}", vehicleModel.Plate);
return Unauthorized();
}
if (vehicleModel == null)
return BadRequest();
await _vehicleService.Modify(vehicleModel);
return Accepted();
}
Код: Выделить всё
public static bool IsUserInRoleForHttpContext(IHttpContextAccessor httpContextAccessor, string roleName)
{
var user = httpContextAccessor.HttpContext?.User;
if (user == null || !user.Identity.IsAuthenticated) // IsAuthenticated always false
return false;
return user.IsInRole(roleName);
}
Код: Выделить всё
httpContextAccessor.HttpContext?.User.Identity.IsAuthenticated
Что я делаю не так?
Подробнее здесь: https://stackoverflow.com/questions/792 ... ways-false