Short problem description: I can't make an internal API call to my controller class after using M365 authentication when starting my app. Retrieving ID or access token has not worked so far and httpClient.GetAsync() method is not accessing the controller class, but rather getting a request message by Microsoft.
My appsettings.json looks like this:
"AzureAd": {"Instance": "https://login.microsoftonline.com/","Domain": "sampledomain.org","TenantId": "sample","ClientId": "sample","CallbackPath": "/signin-oidc","Scope": "api://sample"},I have the following controller class:
[Route("api/[controller]")][ApiController]public class Task_ManagementController : ControllerBase{ private readonly SqlDbContext _dbContext; public Task_ManagementController(SqlDbContext dbContext) { _dbContext = dbContext; } [HttpGet] [Route("Get")] public async Task<List<Task_Management>> Get() { return await _dbContext.Task_Management.ToListAsync(); }}Pretty simple so far.
I am calling the following:
public class CustomHttpClient : HttpClient{ public async Task<T> GetJsonAsync<T>(string requestUri) { HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; }; // Pass the handler to httpclient(from you are calling api) HttpClient httpClient = new HttpClient(clientHandler); var httpContent = await httpClient.GetAsync(requestUri); string jsonContent = httpContent.Content.ReadAsStringAsync().Result; T obj = JsonConvert.DeserializeObject<T>(jsonContent); httpContent.Dispose(); httpClient.Dispose(); return obj; }I checked my requestUri and it's: https://localhost:7076/api/task_management/Get
However, instead of going into my controller class, where I have a breakpoint set, I immediately get a response by https://login.microsoftonline.com/.../.
I have tried many things so far and searched up everything but cant make it work. 4o is also not giving good responses and we end up in circles.
I am using Blazor server with .NET 7. Is there something I am missing? From my understanding so far, I have to add the token to my HttpClient but I don't know how. If you have any hint I would be very happy.