-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wasm] Blazor 303 responses, and fetch vs navigation #39365
Comments
Note: I also tried to implement a HttpClient MessageHandler to see if I could intercept the 303 response and perform a navigation instead, but alas, my MessageHandler doesn't get a look in on the 303 response.
|
Thanks for contacting us. We can't do any of the things you mention because the response is opaque to the code running on the page as per the CORS policy. If you are calling and endpoint that redirects to a different origin you likely need to set the request mode to CORS. See here for details. It is likely that the differences that you are seeing between your request and a form post are because of some headers or contents on the request you are sending vs a regular form post, you can tell this is the case specially if you see the browser make a preflight request, but this is just a hypothesis. |
@javiercn - sorry I do not understand. The issue is, I need to submit a POST request initially, in order to get back a 303 response with a LOCATION uri, which I then need to "navigate in the browser" to, not send a cross site request - but when using blazor / httpclient I can't see any way to do this because it handles it all automatically and doesn't surface the 303 response, not even via a MessageHandler. I think I'll have to construct an html form and not use blazor / http client for this, hopefully a normal Html form submit, will result in the browser submitting a POST request, and handling the 303 response as a normal navigation event, not a fetch request? (not ideal because this means I can't submit the post request via my http client pipeline in C## that does things like appends CORS headers, auth tokens etc). Still I raise this issue, because I don't think it's ideal that I have to break out of the blazor paradigm to try and solve this. |
@dazinator thanks for the clarification. I think you might be able to construct the HttpClientHandler and set the redirections to false? |
@javiercn - that looks like the way forward that I was missing - thank you very much. When creating the new HttpClientHandler I need to be sure to make sure its set up the same as the default one that would usually be provided for me. I only want to set this "AutoRedirect" property not cause other unintended changes. I have put in place the following code now: e.g: services.AddHttpClient(authHeadeHttpClient, (sp, client) =>
{
var navManager = sp.GetRequiredService<NavigationManager>();
client.BaseAddress = navManager.ToAbsoluteUri("");
})
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AllowAutoRedirect = false,
// I want all other properties to be same as default that would normally be created for me.
}); Does blazor / mono just use a new default instance of a HttpClientHandler() or does it do any additional configuration that I'd need to mirror that you are aware of? Thanks |
@javiercn AHHH damn.
|
@dazinator I believe it uses the default. It might be that you need to instantiate the wasmhandler directly, but I can't seem to find the type. @pranavkm can help here. It can be the case that it is not possible to turn off auto-redirects and you might need to raise it up with the dotnet/runtime folks. |
You should be able to configure the requestMessage.SetBrowserRequestOption("redirect", "manual"); |
@pranavkm For anyone else that hits this, you currently need to add the following
|
@pranavkm |
|
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Err... it's not resolved! @javiercn can you re-open? |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Looks like the 'resolved' label needs removing otherwise the bot will keep auto closing :-) |
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
@mkArtakMSFT @pranavkm can one of you please clarify what you believe the bug is for us? |
Tagging subscribers to this area: @dotnet/ncl |
We do not have access to the error code from the fetch API. Do not think this can be resolved from the browser fetch api to bubble this error up. |
Describe the bug
From a blazor wasm application,
Use HttpClient to submit a POST request to web api hosted on the same origin.
Have that API return a 303 response to redirect to an external URL
Blazor automatically follows up on this redirect by submitting a GET request with the browser fetch api. However in this case, due to CORS policies on the external URL being redirected to, CROSS site requests are not allowed. I actually need to fully navigate the user to the new URL in the browser. Due to this Blazor fetch request fails with a CORS error, but only surfaces a generic "fetch has failed" exception back to user code.
Note: This seems to be different behaviour than what you see with ordinary MVC using a form to submit a POST request:
e.g from the ExternalLogin.cshtml in identity:
This result of this POST will result in the browser being navigated to the LOCATION returned in the 303 redirect, which doesn't seem to happen if you use blazor EditForm, and handle the submit via HttpClient to do a POST.
Wondering if it would be more appropriate to either:
Or
To Reproduce
As per above, do a POST request from blazor wasm, and have the server return a 302 with a redirect to an external URL that prevents CORS requests.
Exceptions (if any)
Further technical details
dotnet --info
The text was updated successfully, but these errors were encountered: