Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Graph API working with Web API but throwing error 'Access-Control-Allow-Origin' header is present in Blazor app using same code

$
0
0

I managed to get Microsoft Graph API set up and working in a Web API app where a post request is successful to obtain the bearer token and again to send the email.

However, when I use the same code in Blazor, I get a CORS error when attempting the post request to authenticate and the error is the standard one you get when there is no Cors (I replaced the tenant with TENANT below):

Access to fetch at 'https://login.microsoftonline.com/TENANT/oauth2/v2.0/token' from origin 'https://localhost:7253' 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.

I can't understand what could be different about a Blazor project that would stop this working when it works fine in Web API and both are accessing the same Microsoft URL and only one gets blocked due to CORS on the requested resource aside from the fact that Web API runs from the server and Blazor runs from the browser unless that it is.

This is the part of the code that calls the API:

string? loginEndPoint;loginEndPoint = $"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token";HttpResponseMessage formResponse = await httpClient.PostAsync(loginEndPoint, formParamsEncoded);

I've essentially copied the same config file and same shared class that sends the email from the working Web API so the class that sends the email is identical.

If it isn't possible to make this work from Blazor then it means I will need to build my own Web API that I can add CORS to and call that first to then call the Microsoft API from my API which would be an added layer of complexity that shouldn't really be needed.

I did wonder if I could add NoCors headers from the calling Blazor end using SendAsync instead but that gave a server 400 error rather than the Cors error so not sure if that perhaps got further or not but it would seem SendAsync isn't sending the right sort of request and I'm not sure why:

var request = new HttpRequestMessage(HttpMethod.Post, loginEndPoint);httpClient.DefaultRequestHeaders.Accept.Clear();httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));request.SetBrowserRequestMode(BrowserRequestMode.NoCors);request.SetBrowserRequestCache(BrowserRequestCache.NoStore); //optional request.Content = formParamsEncoded;HttpResponseMessage formResponse = await httpClient.SendAsync(request);

I've already spent hours trying to find a way to get this to work and send the email as it does with Web API projects so would appreciate any help.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>