I have a standalone Blazor Webassembly project that I'm trying to access an Azure Function App from when running locally in Visual Studio like this for example.
response = await Http.GetStringAsync("https://mytestsite897845.azurewebsites.net/api/Test");
My Azure Function App is configured with Azure Active Directory Authentication / Authorization and I have set 'https://localhost:44351' in the Azure Function App CORS and ticked 'Enable Access-Control-Allow-Credentials'.
In the Authentication / Authorization section, if I leave 'Action to take when request is not authenticated' set to 'Allow Anonymous requests (no action)', then the above call works just fine and I get a response from my 'Test' Azure Function.
However as soon as I change 'Action to take when request is not authenticated' to 'Log in with Azure Active Directory', then the above call fails, even after I've logged in with Active Directory B2C. I get the following errors in the browser.
Access to fetch at 'https://login.windows.net/<my client id>/oauth2/authorize?response_type=code+id_token&redirect_uri=https%3A%2F%2Fmytestsite897845.azurewebsites.net%2F.auth%2Flogin%2Faad%2Fcallback&client_id=48fd8563-3826-4d79-b29a-907a17598afe&scope=openid+profile+email&response_mode=form_post&nonce=e428bf7812ef4327a710b1162353ac3d_20200820091540&state=redir%3D%252Fapi%252FTest'
(redirected from 'https://mytestsite897845.azurewebsites.net/api/Test') from origin 'https://localhost:44351' has been blocked
by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves
your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
GET https://login.windows.net/<my client id>/oauth2/authorize?response_type=code+id_token&redirect_uri=https%3A%2F%2Fmytestsite897845.azurewebsites.net%2F.auth%2Flogin%2Faad%2Fcallback&client_id=48fd8563-3826-4d79-b29a-907a17598afe&scope=openid+profile+email&response_mode=form_post&nonce=e428bf7812ef4327a710b1162353ac3d_20200820091540&state=redir%3D%252Fapi%252FTest
net::ERR_FAILED
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: TypeError: Failed to fetch
System.Net.Http.HttpRequestException: TypeError: Failed to fetch
at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts, CancellationToken callerToken, Int64 timeoutTime)
at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask, CancellationToken cancellationToken)
at BACClient.Pages.Counter.OnInitializedAsync() in .\Pages\Counter.razor:line 79
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
Anyone know what I'm missing?