Hello.
I use a DataService from client-side and when I call API it's always Response status code does not indicate success: 401 (Unauthorized)..
But when i call the very same API from the code-behind ( razor.cs) it's works #1.
I think it's related to the thhpcontext. Any suggestion?
My service :
private readonly HttpClient _httpClient;
public StageStatutDataService(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IEnumerable<StageStatut>> GetAllStageStatuts()
{
return await JsonSerializer.DeserializeAsync<IEnumerable<StageStatut>>
(await _httpClient.GetStreamAsync($"api/stagestatut"), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
}
My API :
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class StageStatutController : ControllerBase
{
private readonly IStageStatutRepository _stageStatutRepository;
public StageStatutController(IStageStatutRepository stageStatutRepository)
{
_stageStatutRepository = stageStatutRepository;
}
[HttpGet]
public IActionResult GetAllStageStatuts()
{
return Ok(_stageStatutRepository.GetAllStageStatuts());
}