-
Describe the bugUse HttpClient to post request to another site will result in exception To Reproduce
Exceptions (if any)
Further technical details
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 10 replies
-
Is there a question? Blazor wasm has the same restrictions running JavaScript in the browser (it all runs in the same sandbox) |
Beta Was this translation helpful? Give feedback.
-
Are you saying that this issue is by design? I can see the same snippet works fine in Blazor server app, console app, desktop app. Can you elaborate more on why Blazor wasm doesn't work? |
Beta Was this translation helpful? Give feedback.
-
Yup. Blazor WebAssembly runs in your browser where particular security features are forced. One of them you met. If you run Blazor Server Side then your code runs (communicates other sites) at your server - which is not your browser - and send the DOM changes to your browser via SignalR. That is the difference. It is a SO article explains some details, there are might be better articles about it. |
Beta Was this translation helpful? Give feedback.
-
Thanks, very useful and clear for me. So in my issue, the only way it can work is the other site (https://login.microsoftonline.com/xxxxxxxxxxx/oauth2/token) must return response with header including Access-Control-Allow-Origin = "*", right? |
Beta Was this translation helpful? Give feedback.
-
You can use CORS in Blazor Webassembly as well as configuring other |
Beta Was this translation helpful? Give feedback.
-
Hi @javiercn ,below is my code where i am setting the default browser as CORS but still getting the same error. |
Beta Was this translation helpful? Give feedback.
-
Need this as well pls |
Beta Was this translation helpful? Give feedback.
-
I'm trying to get data from an IoT device and the only thing that doesn't throw is The request payload is something like:
And the device replies with:
However, there's no body. The device replies with 200 and if I issue the same request in Fiddler, I get the response body with the same 125 bytes length. If I set Fiddler as a proxy, I can see in Fiddler the content. |
Beta Was this translation helpful? Give feedback.
Hi @javiercn ,below is my code where i am setting the default browser as CORS but still getting the same error.
Could you please give us your working code repo.
builder.Services.AddTransient(sp => new HttpClient(new DefaultBrowserOptionsMessageHandler(new WebAssemblyHttpHandler())
{
//DefaultBrowserRequestCache = BrowserRequestCache.NoStore,
//DefaultBrowserRequestCredentials = BrowserRequestCredentials.Omit,
DefaultBrowserRequestMode = BrowserRequestMode.Cors,
})
{
BaseAddress = new Uri(builder.HostEnvironment.BaseAddress),
});