3

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?

1
  • just wondering if you ever figured this issue out? I'm running into the same problem myself and its driving me nuts Commented Feb 2, 2022 at 22:59

1 Answer 1

1

Can you please share steps that you took to configure Azure Function to authenticate against Azure B2C (Policy and Application)? The steps you mentioned are not related to Azure AD B2C but to Azure AD.

You may find this link useful which explains the actual setup needed: https://codemilltech.com/adding-azure-ad-b2c-authentication-to-azure-functions/#:~:text=The%20Azure%20Function%20App%20can,And%20then%20the%20Xamarin.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.